2015-11-03 38 views
0

我有代碼,添加了文本標籤,字幕和像這樣的附件圖標:添加文本或繪圖到的UITableViewCell

cell.textLabel.text = @"Title"; 
cell.detailTextLabel.text = @"Subtitle"; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

// And I see code in the docs for an image: 
cell.imageView.image = ... 

但我想在圖像去的地方(左側)成爲一個文本或標籤(如在Instagram中)或繪製圓圈(如Apple收藏夾電話屏幕)。這是如何完成的?

+0

用故事板或XIB來設計你的 – SomeGuy

+0

是有辦法做到這一點編程? – cdub

+0

以編程方式創建自定義單元格:http://www.mysamplecode.com/2013/06/ios-programmatically-create-custom-cell.html – NeverHopeless

回答

4

雖然與UITableViewCellStyleDefaultUITableViewCell免費獲得imageView財產,你可以用它,爲了以防萬一,你想擁有對imageView位置和標籤對細胞的精細控制,你需要去定製UITableViewCell。下面是關於如何實現這一目標的步驟:

步驟1:創建一個新的子類UITableViewCellMyCustomCell

@interface MyCustomCell : UITableViewCell 

步驟2:MyCustomCell.m實施initWithStyle:reuseIdentifier:方法,並添加任何自定義視圖中的單元格內容視圖。

- (id)initWithStyle:(UITableViewCellStyle)iStyle reuseIdentifier:(NSString *)iReuseIdentifier { 
    if ((self = [super initWithStyle:iStyle reuseIdentifier:iReuseIdentifier])) { 
     MyView *myCustomView = [[MyView alloc] init]; 
     myCustomView.frame = CGRectMake(6.0f, 6.0f, 30.0f, 30.0f); 
     [self.contentView addSubview:myCustomView]; 
    } 

    return self; 
} 

第3步:實現layoutSubviews方法來對你的內容的子視圖精細控制。

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.imageView.frame = CGRectMake(6.0f, 6.0f, 30.0f, 30.0f); 
    GFloat textLabelXPosition = self.imageView.frame.origin.x + self.imageView.frame.size.width + 10; 
    self.textLabel.frame = CGRectMake(textLabelXPosition, 0.0, contentRect.size.width - textLabelXPosition, contentRect.size.height); 
} 

第4步:在你的表視圖控制器的cellForRowAtIndexPath:方法最後使用MyCustomCell實例。

0

創建一個ImageView的image,然後添加以下行到您的cellForRowAtIndexPath

CGRect newFrame; 
newFrame.origin.x = self.accessoryView.frame.origin.x; 
newFrame.origin.y = self.accessoryView.frame.origin.y; 

self.image.frame= newFrame; 

這會給你的訪問附屬視圖的座標。現在覆蓋您的文字/標籤。在廈門國際銀行 創建一個標籤 - yourLabel

self.yourLabel.frame= newFrame;