xcode UITableView 出现空单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10479985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:12:38  来源:igfitidea点击:

UITableView appears with empty cells

iosobjective-cxcodeuitableviewcocoa-touch

提问by Oleg

I have a problem displaying UITableView: Some cells are empty, and content in cells becomes visible only after scrolling (if empty cell scrolls out of the screen and then goes back). I Can't understand what might be the problem.

我在显示时遇到问题UITableView:某些单元格为空,并且单元格中的内容仅在滚动后才可见(如果空单元格滚动出屏幕然后返回)。我不明白可能是什么问题。

Here is my code:

这是我的代码:

- (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;
}

Thank you in advance for your suggestions!

预先感谢您的建议!

采纳答案by adig

Well, you have the cell customization code that happens before you create the cell.

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

Change it like this :

像这样改变它:

- (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;
}

回答by Jpellat

The problem is that you make

问题是你让

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

before

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

change the order. What is happening is that the first time you execute the table View you never do the title before the alloc. When you reuse cell it works because the cell != nil

改变顺序。发生的事情是,第一次执行表视图时,您永远不会在分配之前执行标题。当您重用单元格时,它会起作用,因为单元格 != nil

回答by Lorenzo B

You need to put these lines

你需要把这些行

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

after the if(){...}condition.

之后的if(){...}状态。

In the first pass cells are nil. Put those lines before the ifdoes nothing. This is why you see empty cells.

在第一次传递中,单元格为零。将这些行放在if什么都不做之前。这就是您看到空单元格的原因。

A simple question

一个简单的问题

Why do you call [self.filesList retain]?

你为什么打电话[self.filesList retain]

Hope it helps.

希望能帮助到你。