Ios UITableView通过单击添加更多单元格

Ios UITableView通过单击添加更多单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在UITableView的最后一个单元格中添加了UIButton,我想通过单击显示更多单元格 - (void)viewDidLoad { [super viewDidLoad]; rowCount = 15; dataArray = [[NSMutableArray alloc] init]; for (int i = 0; i < 100; i++) { NSString *value = [NSString stringWithFor

我在UITableView的最后一个单元格中添加了UIButton,我想通过单击显示更多单元格

- (void)viewDidLoad {
    [super viewDidLoad];

    rowCount = 15;
    dataArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 100; i++) {
        NSString *value = [NSString stringWithFormat:@"the value of row is: %d", i +1];
        [dataArray addObject:value];
    }

}

一开始它工作正常,但当我滚动表格时,单元格没有改变


我想在最后一个单元格显示按钮,如果您使用两个不同的单元格,则效果会更好,否则请替换CellForRowatineXpath中的代码

if (indexPath.row == rowCount -1){}

[cell.textLabel setText:dataArray[indexPath.row]];
在另一部分

并将代码放在UITableViewCell*单元格的正下方

for(UIView *view in cell.view.subviews){
   if([view isKindOfClass: [UIButton class]]){
[view removeFromSuperView];
   }
}

我认为您需要刷新可见单元格,具体操作如下:

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] 
             withRowAnimation:UITableViewRowAnimationNone];
还有一个更好的方法是,你们有两个原型单元,一个用于数据,一个带有按钮。让我们假设您给出第二个标识符“buttonCell”,然后您只需执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idintify = (indexPath.row < rowCount -) ? @"Cell" : @"buttonCell";
    UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:idintify];

    if (cell == nil) //initialize 

    if (indexPath.row < rowCount -1) {
        [cell.textLabel setText:dataArray[indexPath.row]];

    }

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*IDitify=(indexath.row

didSelectRowAtIndexPath
中,如果用户滚动时重复使用表视图中的单元格,则增加
rowCount
,并重新加载数据。当您向单元格原型的实例中添加一个按钮而不删除它时,该按钮将保留,即使稍后在另一个索引中使用该单元格也是如此。这将导致屏幕截图上显示的内容

您应该在interface builder中创建两个单元格原型,您的cellForRow:atIndexPath:应该如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return cell with data

    if (indexPath.row < dataArray.count) {
        UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:@"cell_prototype_1"];
        [cell.textLabel setText:dataArray[indexPath.row]];
        return cell;
    }     

    // If it's the last index, return cell with button

    else {
        UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:@"cell_prototype_2"];
        return cell;
    }

    // This won't get called

    return [UITableViewCell new];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//返回包含数据的单元格
if(indexath.row
如果可以的话,您应该避免放置这样的按钮,而是在用户滚动到页面末尾时开始加载更多内容list@wissam我看你的代码没有问题。你能提供你的.m类/ir的代码一个.m类的链接吗,这样我们就可以检查是否还有其他的mistake@wissamiOS建议:如果你使用故事板和原型按钮,尝试使用两个按钮,一个用于显示数据,另一个仅用于按钮。@Jeev这是一个单元格重用问题,没有其他问题。@sumit Garg我相信在故事板中使用两个按钮而不是一个按钮会有所帮助。该表正在创建问题/或无法为最后一个按钮刷新自身。2个按钮的概念将照顾到重新加载的行,并且不会有什么不同。我总是建议在原型中使用两个不同的单元。在单元格中为INdexpath声明自己的Uicompoenets;TableView创建resue Problemfor(cell.view.subviews中的UIView*视图){也很好,但是for循环和if必须再次运行并again@Jeev我知道这不是一个好方法,但我已经定义了两个原型单元,以便更好地实现。
[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] 
             withRowAnimation:UITableViewRowAnimationNone];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idintify = (indexPath.row < rowCount -) ? @"Cell" : @"buttonCell";
    UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:idintify];

    if (cell == nil) //initialize 

    if (indexPath.row < rowCount -1) {
        [cell.textLabel setText:dataArray[indexPath.row]];

    }

    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return cell with data

    if (indexPath.row < dataArray.count) {
        UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:@"cell_prototype_1"];
        [cell.textLabel setText:dataArray[indexPath.row]];
        return cell;
    }     

    // If it's the last index, return cell with button

    else {
        UITableViewCell *cell = [self.myTable dequeueReusableCellWithIdentifier:@"cell_prototype_2"];
        return cell;
    }

    // This won't get called

    return [UITableViewCell new];
}