ios - 在不重新加载整个表的情况下更新 UITableView 中的特定行

标签 ios label tableview

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

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

最佳答案

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

这是一个示例:

@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 正在重复使用每个表格单元格以获得更好的性能和更好的内存管理。因此,如果您遇到诸如为什么我的其中一行的值在其他行中重复的情况,那是因为 tableCell 的实例在其他行中被重用了。

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

希望这能帮助您解决问题。

关于ios - 在不重新加载整个表的情况下更新 UITableView 中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37803133/

相关文章:

html - 标签跨度和斜体字体属性的奇怪行为

ios - 如何根据iOS App中的屏幕大小更改tableVIew高度

c++ - 在 View 中用 ID 替换字符串的简单方法

ios - 如何限制 AVAssetWriter 在编码视频时使用过多内存?

ios - 如何在 NSNotification 类中选择一个选择器?

android - iPhone 无法从我的网页(服务器)播放视频

r - 在堆叠的 ggplot 条形图中标记单个条形

jquery - 无法获得 <label> 元素的正确宽度或outerWidth

Javafx 使用 tableview 更新数据库

iphone - 如何使用 GDataXML 解析器解析此 xml?