我在创建的 UITableViewCell 中显示数据时遇到问题。
我有一个 CoreData 模型,我有一个从 Xib 加载的自定义单元格。布局正确加载并生成正确数量的单元格。更新 button.titleLabel.text 和描述时出现问题。
下面是我的uitableview和uitableviewcell相关代码:
#pragma mark - UITableViewDatasource Methods.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [pointsHistoryItems count];
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
PointsHistoryItemCell* cell = (PointsHistoryItemCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return [self createCustomCell:cell cellForRowAtIndexPath:indexPath];
}
- (PointsHistoryItemCell *)createCustomCell:(PointsHistoryItemCell *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = (PointsHistoryItemCell *)[[[NSBundle mainBundle] loadNibNamed:@"PointsHistoryItemCell" owner:self options:nil] objectAtIndex:0];
}
AwesomePointsHistoryItem *aphi = (AwesomePointsHistoryItem *)[pointsHistoryItems objectAtIndex:indexPath.row];
cell.description.text = aphi.description;
NSString* pointsText = [NSString stringWithFormat:@"%@ Points", [aphi.points stringValue]];
[cell.button.titleLabel setText:pointsText];
NSLog(@"is reaching createCustomCell");
NSLog(@"points %@", cell.button.titleLabel.text);
return cell;
}
日志打印:
is reaching createCustomCell
points 600 Points
但是.. cell.button.titleLabel.text 不会更新!
我做错了什么?
最佳答案
使用
[cell.button setTitle: pointsText forState: UIControlStateNormal];
代替
[cell.button.titleLabel setText:pointsText];
注意:希望 cell.button 是一个 UIButton
关于ios - 核心数据单元未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21725102/