Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当我将某个单元格id的行添加到UITableView时,它将返回一个空行_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 当我将某个单元格id的行添加到UITableView时,它将返回一个空行

Ios 当我将某个单元格id的行添加到UITableView时,它将返回一个空行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个自定义的UITableView,具有不同的单元格ID。我试图做的是在选择按钮时添加一行。这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellID; ... else if ([indexPath row] == 5) { cellID = @"r

我有一个自定义的
UITableView
,具有不同的单元格ID。我试图做的是在选择按钮时添加一行。这是我的密码:

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

表视图有数据源,数据源几乎总是(让我们说总是)数组。数组至少描述了每行的基本内容。首先,声明表的数据源(aka模型):

为了简单起见,我们可以说,描述行所需的唯一东西就是重用标识符。从注释中可以看出,您希望表以10行开始,第8行之前的行具有唯一的重用标识符,其他行的标识符为@“rowSix”。如果是这样,下面介绍如何在viewDidLoad中初始化数据源:

self.rowArray = [@[@"rowOne", @"rowTwo", @"rowThree", @"rowFour", @"rowFive", @"rowSix",
    @"rowSeven", @"rowEight", @"rowSix", @"rowSix"] mutableCopy];
这个冗长的数据源定义现在简化了代码的其余部分。例如,
numberOfRowsInSection
现在看起来如下所示:

return self.rowArray.count;
cellforrowatinexpath
则更好:

NSString *cellID = self.rowArray[indexPath.row];
// delete error-prone, unreadable, slow if else if else if logic
customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
即使您的按钮按下逻辑也会改进:

- (IBAction)addRow:(id)sender {
    // probably no longer need self.myNum
    // probably no longer need self.numberOfRows
    [self.rowArray addObject:@"rowSix"];
    [self.myTableView reloadData];
}
很酷,对吧

PS-这条线

else if ([indexPath row] >= self.myNum && [indexPath row] <= self.myNum)

else如果([indexPath行]>=self.myNum&&[indexPath行],由于错误消息,这是一个更好的问题。我可以帮助理解逻辑,但请删除此问题或此重复()。刚刚删除了另一篇博文。非常感谢。如果用户按此添加按钮9次,您希望看到9行?并且希望这些行填充不同类型的单元格(具有不同的重用标识符)对于最大值的每一行,比如如果这些是重用ID,那么您会想要:一、二、三、四、五、六、六、六,等等??如果我不够清楚,很抱歉。我在
viewDidLoad
中将
numberOfRows
设置为9。我试图做的是,当用户选择一个按钮时,它应该添加另一个带有标识符
rowSi的单元格x
。开始初始化后有多少行?非常感谢!!我一整天都在尝试这个!!而不是
[self.myTableView reloadData]
,我做了
插入rowatindexpath…
因为我想要动画。当我这样做时,单元格大小变为非常紧凑的大小。但是当单元格消失后,又恢复正常。你知道为什么会发生这种情况吗?你可能需要实现-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath,返回特定自定义单元格的高度。请确保表的委托指针也设置为实现此方法的vc。
- (IBAction)addRow:(id)sender {
    // probably no longer need self.myNum
    // probably no longer need self.numberOfRows
    [self.rowArray addObject:@"rowSix"];
    [self.myTableView reloadData];
}
else if ([indexPath row] >= self.myNum && [indexPath row] <= self.myNum)