【问题标题】:DataArray changed but on UITableView not show new data [closed]DataArray 已更改,但在 UITableView 上不显示新数据 [关闭]
【发布时间】:2013-07-04 11:27:22
【问题描述】:

我调试 DataArray 已更改,但在 UITableView 上仍然没有显示从 DataArray 获取的新数据。 这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
        FileNameLabel.backgroundColor = [UIColor clearColor];
        FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
        FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
        FileNameLabel.textColor = [UIColor blackColor];
         NSLog(@"File Temp 4 array: %@", temp);
        FileNameLabel.text =[temp objectAtIndex:indexPath.row];
        [cell.contentView addSubview: FileNameLabel];
        [FileNameLabel release];

    }
        return cell;
}

ViewWillAppear 中作用update()

-(void) update
{
      if([FileCompletedArray count] != [temp count])
      {
            temp = [FileCompletedArray mutableCopy];
            NSLog(@"File Temp 1 array: %@", temp);
            [_tableView reloadData];
            NSLog(@"File Temp 2 array: %@", temp);
       }
}

你有解决办法吗?

【问题讨论】:

标签: ios objective-c iphone uitableview cocoa-touch


【解决方案1】:

调用reloadData后,cellForRowAtIndexPath会被再次调用,但是由于cell已经创建,tableview会重用cell,所以这里正确的方法是获取cell里面的label,更新@之外的text 987654321@ 块。我已经修改了你的代码,更新的代码如下。

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    FileNameLabel.tag = 1000;
    FileNameLabel.backgroundColor = [UIColor clearColor];
    FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
    FileNameLabel.textColor = [UIColor blackColor];
     NSLog(@"File Temp 4 array: %@", temp);
    [cell.contentView addSubview: FileNameLabel];
    [FileNameLabel release];

}


UILabel *fileNameLbl = (UILabel*)[cell.contentView viewWithTag:1000];
fileNameLbl.text =[temp objectAtIndex:indexPath.row];

请检查这是否解决了您的问题。

【讨论】:

    【解决方案2】:

    这是一个单元格重用问题,因为您设置单元格文本 (FileNameLabel.text =[temp objectAtIndex:indexPath.row];) 的代码仅在您创建新单元格实例时运行。

    您需要区分创建新单元格时需要哪些设置,以及重用/准备显示单元格时需要哪些设置。

    【讨论】:

      【解决方案3】:

      如果您有不同的部分,则使用以下代码将值分配给FileNameLabel

      FileNameLabel.text =[[temp objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
      

      【讨论】:

        猜你喜欢
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        相关资源
        最近更新 更多