2013-02-25 26 views
1

我想知道是什麼使一個的UITextField適合一個UITableView細胞的最佳途徑。最佳路線,以適應的UITextField到的UITableView細胞

我已經使用這個方法之前:

@implementation UITextField (custom) 
- (CGRect)textRectForBounds:(CGRect)bounds { 
    return CGRectMake(bounds.origin.x + 0, bounds.origin.y + 10, 
         bounds.size.width - 30, bounds.size.height - 16); 
} 
- (CGRect)editingRectForBounds:(CGRect)bounds { 
    return [self textRectForBounds:bounds]; 
} 
@end 

但是這會導致一個問題:

Category is implementing a method which will also be implemented by its primary class 

雖然我見過的方式來隱藏這些警告,感覺這更是一個黑客攻擊比正確的方法。

什麼是適合的UITextField到細胞的最佳途徑。這是我的領域:

// Username field 
usernameField = [[UITextField alloc] initWithFrame:(CGRectMake(10, 0, 300, 43))]; 
usernameField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

它輸入到這樣的小區:

if (indexPath.row == 0) { 
     [cell.contentView addSubview:usernameField]; 
    } 

圖片: enter image description here

回答

0

我建議通過重寫你的自定義的UITableViewCell的layoutSubviews方法這樣做。那裏更簡單。喜歡的東西:

- (void)layoutSubviews 
    { 
     [super layoutSubviews]; 
     float w, h; 

     w = (int)(self.frame.size.width - 30);   
      h = (int)(self.frame.size.height - 16); 

     [self.detailTextLabel setFrame:CGRectMake(0, 10, w, h)]; 
    } 

這裏的自定義單元初始化

@interface MyCustomCell: UITableViewCell { 

    } 
    @end 

    @implementation MyCustomCell 


    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    { 
     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
      } 
      return self; 
    } 

這裏的片段年代自定義單元格將被創建,其中的一個片段:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 

     MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     } 
+0

你能解釋一下如何,請? – StuartM 2013-02-25 22:42:59

+0

剛剛添加了有關layoutSubviews方法的詳細信息。我假設你已經有一個自定義的UITableViewCell? – 2013-02-25 22:45:35

+0

這會影響所有子視圖嗎?我編程設置我所有的子視圖,這會影響你明確設置的UITableViewCell所有的人或僅文本字段 – StuartM 2013-02-25 22:48:14