简体   繁体   English

在新视图中的tableview中插入一行

[英]Inserting a row in a tableview in a new section

I start with a tableview with one section, the first section always has three rows. 我从带有一个部分的tableview开始,第一部分总是有三行。 After some content has downloaded, I want show a new cell, in a second section, so I do : 下载了一些内容后,我想在第二部分中显示一个新单元格,所以我这样做:

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

But I get an NSInternalConsistencyException: 但我得到一个NSInternalConsistencyException:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:无效的节数。 The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' 更新后的表视图中包含的节数(2)必须等于更新前的表视图中包含的节数(1),加上或减去插入或删除的节数(0插入,0删除)。”

Why isn't it inserted the new section automatically, If I do insert a section, then I get problems when inserting rows that are not in new sections. 为什么不自动插入新的部分,如果我插入一个部分,那么在插入不在新部分中的行时会出现问题。

Why can't it just add the new section automatically? 为什么不能自动添加新的部分? surely InsertRowAtIndexpath: also include inserting a section if necessary as an NSIndexPath also includes a section. 当然,InsertRowAtIndexpath:还包括在必要时插入一个部分,因为NSIndexPath还包含一个部分。

[self.tableView insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic];

and don't forget to set numberOfSectionsInTavleView and numberOfRowsInSection accordingly. 并且不要忘记相应地设置numberOfSectionsInTavleView和numberOfRowsInSection。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    NSInteger numberOfSections;
    if (condition1) {
        numberOfSections = 2;
    } else if (condition2) {
        numberOfSections = 3;
    } else {
        numberOfSections = 4;
    }

    return numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section
    if(section == 1) return 3;
    else return numberOfRows;
}

Keep this in ViewDidLoad 将其保存在ViewDidLoad中

childArray1 = [[NSMutableArray alloc] initWithObjects: @"Apple", @"Microsoft", @"HP", @"Cisco", nil];
childArray2 = [[NSMutableArray alloc] init];
parentArray = [[NSMutableArray alloc] initWithObjects:childArray1, nil];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return [parentArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      NSInteger rowCount ;
      if (section == 0)
      {
           rowCount = [childArray1 count];
      }
      else
      {
           rowCount = [childArray2 count];
      }
      return rowCount;
}

Once your content is downloaded from server. 从服务器下载内容后。

 if([childArray2 count] > 0)
     [parentArray addObject : childArray2];
 [tableView reloadData];

This should work for you. 这应该适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM