将某个单元格id的行添加到UITableView:返回一个空行 [英] Add row of a certain cell id to a UITableView: returns an empty row

查看:80
本文介绍了将某个单元格id的行添加到UITableView:返回一个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有不同单元ID的自定义`UITableView`。我正在尝试在按钮被选中时添加一行。这是我的代码:



I have a custom `UITableView` with different cell id's. I'm trying to add a row when the button gets selected. Here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.numberOfRows;
}

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

    NSString *cellID;

    ...
    else if ([indexPath row] == 5) {
        cellID = @"rowFive";
    }
    else if ([indexPath row] >= self.myNum && [indexPath row] <= self.myNum) {
        cellID = @"rowSix";
    }
    else if ([indexPath row] == self.myNum + 1) {
        cellID = @"rowSeven";
    }
    else if ([indexPath row] == self.myNum + 2) {
        cellID = @"rowEight";
    }

    customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[customCell alloc] init];
    }

    return cell;
 }

- (IBAction)addRow:(id)sender
{
    self.myNum ++;
    self.numberOfRows ++;
    [self.myTableView reloadData];
}





当我选择按钮时,会添加一行,但内部没有任何内容它的。它只是一个空行,并没有加载`rowSix`的单元格ID。



当我删除`cell == nil的if语句时`,然后,当我选择按钮时,应用程序崩溃并出错。



What happens is, when I select the button, a row gets added, but nothing inside of it. It's just an empty row, and it doesn't get loaded with the cell id of `rowSix`.

When I delete the if statement of `cell == nil`, then, when I select the button, the app crashes with an error.

推荐答案

你编码的是绝对的。在cellForRowAtIndexPath中设置一个断点:你会看到它。如果创建新单元格,则所有值都初始化(或为空)。你必须用一些值填充它。



你是否在使用过的tableview中的界面构建器中创建了这个单元格类型。



PS:你的编码风格很混乱。更干净的风格有助于避免错误和构建好的应用程序
It is absolut that what you have coded. Set a breakpoint in cellForRowAtIndexPath: and you will see it. If you create a new cell all values are as initialized (or empty). You must fill it with some values.

Have you created that cell type in the interface builder in the used tableview.

PS: your coding style is messy. A cleaner style helps avoiding bugs and building good apps


这篇关于将某个单元格id的行添加到UITableView:返回一个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆