ios - 核心数据单元未正确更新

标签 ios uitableview core-data

我在创建的 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/

相关文章:

ios - 如何在日历中获取上个月和下个月-Swift

ios - (SpriteKit + Swift 2) - 当用户点击它时移除 Sprite

ios - 在 IOS 中从国家名称获取国家代码

iphone - 通过 wifi 在 iOS 设备之间同步核心数据?

ios - SwiftUI:当 "Dynamic List with groups"属性更改时, `@Published` 未刷新

ios - UITableViewCell 中的 ScrollView 不会保存位置

iphone - 如何在 UITableview 的部分中对 NSarray 进行排序

ios - 调用出队单元格时出队可重用单元格崩溃

ios - Core Data 保存时性能不佳 : and [UITableView endUpdates]

ios - 如何在 Core Data 中使用派生属性