更新 UITableView 中的特定行而不重新加载整个 Table

Updating A Specific Row In UITableView Without Reloading The Whole Table

我想在不重新加载数据的情况下更新表视图中标签的文本。 我试试((uilabel *)[self.tableview viewWtihTag:10]).text = Newstring 但它不起作用。

是否正确?或者有其他解决方案?

尝试重新加载 table 的特定单元格 ...

NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
[myUITableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:rowToReload, nil] withRowAnimation:UITableViewRowAnimationNone];

并添加代码以更改 cellForRowAtIndexPath:

中的文本
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

使用这个方法。这可以帮助您解决问题

使用这个方法- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation是正确的。但是,您应该跟踪要在每个单元格的每一行中加载的值,然后在用户滚动 table.

时每次重新加载一行时访问它们

这是一个示例:

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property NSMutableArray *labelValueListForSection0;
@property NSMutableArray *labelValueListForSection1;
@property NSMutableArray *labelValueListForSection2;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _labelValueListForSection2 = [[NSMutableArray alloc] initWithObjects:@"value1", @"value2", @"value3", nil];
}

- (void)changeAnItem
{
    [_labelValueListForSection2 setObject:@"ChangedValue" atIndexedSubscript:1];

    [_table reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"SimpleTableItem";

    UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if(tableCell == nil)
    {
        tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    switch(indexPath.section)
    {
        case 0:

            tableCell.textLabel.text = @"Test";

            break;

        case 1:

            tableCell.textLabel.text = @"Test";

            break;

        case 2:

            tableCell.textLabel.text = [_labelValueListForSection2 objectAtIndex:indexPath.row];

            break;

        case 3:

            tableCell.textLabel.text = @"Test";

            break;

        case 4:

            tableCell.textLabel.text = @"Test";

            break;

        default:
            break;
    }

    return tableCell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

@end

如您所见,我跟踪每一行的值并将它们存储在一个数组中。在我的例子中,我有一个特定部分的每一行的值数组。所以当这个方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

被调用,它将获取应该分配给该行的值。

如果您不知道,iOS 正在重用每个 table 单元以获得更好的性能和更好的内存管理。因此,如果您遇到诸如为什么我的其中一行的值在其他行中重复的情况,那是因为 tableCell 的实例在其他行中被重用了。

所以,为了确保每次加载一个单元格,并且值应该是正确的。您必须跟踪每一行的值,并在每次重新加载时将其重新分配给该单元格。

希望本文能帮助您解决问题。