とてもシンプルです:
最初にカスタムセルにラベルのプロパティを作成します
カスタムセル.h
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *yourLabel;
TableViewController に customcell のインスタンスを作成します
YourTableViewController.h
@interface YourTableViewController : UITableViewController<
{
CustomCell *cell;
}
および YourTableViewController.m で
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.yourLabel.text = @"whatever you want to add";
今、これを行う以外の方法でcustomcellのラベルを更新したい場合。
-(void)someMethod()
{
CustomCell *acell = (CustomCell *)[tableView cellForRowAtIndexPath:n];
acell.yourLabel.text = @"whatever you want to add.";
}