2009-11-18 40 views
4

我想用initWithStyle自定義TableViewCell,因爲它表示initWithFrame在3.0之後被棄用。以前一切正常工作與initWithFrame。如何使用initWithStyle 3.0後自定義TableViewCell

是否有任何教程或示例代碼可用於此?謝謝。

+0

我也想知道這一點。如果只有沒有任何隱式視圖的樣式... – 2009-12-04 07:17:54

回答

7

我有子類UITableViewCell,然後重寫initWithStyle方法。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
     self.selectionStyle = UITableViewCellSelectionStyleNone; 

     // Initialization code 
     msgText = [[UILabel alloc] init]; 
     [self.contentView addSubview:msgText]; 
    } 
    return self; 
} 

msgText是該類的UILabel屬性,我在其他地方設置了標籤的text屬性。你可以添加任何視圖到你喜歡的self.contentView。當我添加文本和/或圖像等內容時,我還設置了每個子視圖的框架。

+0

您還可以在layoutSubviews中定義元素的位置嗎? tnx – 2012-01-23 23:35:58

+1

絕對如此。將要添加到UITableViewCell子視圖的元素的框架設置爲相對於UITableViewCell的左上角。 – acqu13sce 2012-01-24 03:58:13

+1

initWithStyle方法不會爲我調用。你在tableView的cellForRowAtIndexPath中做什麼? – Michael 2012-10-16 08:10:53