如何动态添加Section和TableView Cell

时间:2012-04-02 03:04:35

标签: iphone objective-c ios tableview tableviewcell

我有一个按钮,当按下该按钮时,将添加一个tableview部分,并且还会向该部分添加一个新行。我该如何以编程方式实现它?

我有一系列细胞。

这是我的代码

- (IBAction)addCell:(id)sender {

    UITableViewCell *newCell = [[UITableViewCell alloc] init];

    counter++;
    [cells addObject:newCell];

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [cells count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      //this is where i want to adjust the row per section
      //this code hide the recent sections, only the current created section appears
      return [cells objectAtIndex:indexPath.row];
}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

首先,更新您的数据模型(NSArray或mutable)。其次,当你想要tableview刷新时,添加一个代码[self.tableView reloadData];


oops,你的代码有些奇怪的uitaleviewDataSource,Delegate Method。

另外,你有一些错误。

为什么你实现numberOfRowsInSection返回1?很奇怪。

我赞赏以下代码。

案例1.没有DataModel。

- (IBAction)addCell:(id)sender 
{

    cellRowsCount++;

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return cellRowsCount;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
       reuseIdentifier:CellIdentifier];
      }

     return cell;
   }

您的代码,dataModel(NSArray或mutable)不一定需要,所以我只是添加了rowCount变量,并且tableviewCell的rowsCount已经同步。

如果你想使用DataModel,请在CASE2下面。请参阅plz。


CASE2。有DataModel。

- (IBAction)addCell:(id)sender 
{

    textCount ++;

    NSString *cellText = [NSString stringWithFormat:"blah %d", textCount];
    [myArray addObject:cellText];

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return [myArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [[myArray objectAtIndex:section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  if (cell == nil) 
  {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:CellIdentifier];
  }

  cell.textLabel.text = (NSString *)[myArray objectAtIndex:indexPath.row];

  return cell;
}

答案 2 :(得分:0)

您只需将新部分和新项目添加到包含该部分的数组以及类似的项目中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    animals = @{@"Section 1" : @[@"Item 1", @"Item 2", @"Item 3"],
                @"Section 2" : @[@"Item 1", @"Item 2"]};
}

之后,只需在逻辑需要的地方重新加载tableView:

[self.tableView reloadData];