加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – UITableView出现空单元格

发布时间:2020-12-14 18:02:34 所属栏目:百科 来源:网络整理
导读:我在显示UITableView时出现问题:某些单元格为空,单元格中的内容仅在滚动后才会显示(如果空单元格滚出屏幕然后返回).我无法理解可能是什么问题. 这是我的代码: - (void)viewDidLoad{ [super viewDidLoad]; [self updateFilesList]; [tableView reloadData];
我在显示UITableView时出现问题:某些单元格为空,单元格中的内容仅在滚动后才会显示(如果空单元格滚出屏幕然后返回).我无法理解可能是什么问题.

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self updateFilesList];
    [tableView reloadData];
}

- (void) viewDidAppear:(BOOL)animated
{
    animated = YES;
    [self updateFilesList];
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.filesList retain];

    NSString *title = [self.filesList objectAtIndex:indexPath.row];
    title = [title stringByDeletingPathExtension];
    title = [title lastPathComponent];
    if (title.length >33) {
        title = [title substringFromIndex:33];
    }

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    [cell.imageView setImage:[UIImage imageNamed:@"oie_png-1.png"]];
    cell.textLabel.text = title;

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

    return cell;
}

提前感谢您的建议!

解决方法

好吧,您有在创建单元格之前发生的单元格自定义代码.

像这样改变它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.filesList retain];

    NSString *title = [self.filesList objectAtIndex:indexPath.row];
    title = [title stringByDeletingPathExtension];
    title = [title lastPathComponent];
    if (title.length >33) {
        title = [title substringFromIndex:33];
    }

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // This part creates the cell for the firs time
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // This part customizes the cells
    [cell.imageView setImage:[UIImage imageNamed:@"oie_png-1.png"]];
    cell.textLabel.text = title;


    return cell;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读