3

我有一个按钮,当按下该按钮时,将添加一个 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];
}
4

3 回答 3

1

嗨试试看这个:

如何以编程方式显示 UITableView?

以编程方式将单元格添加到 UITableView


希望这可以帮助你。

于 2012-04-02T03:24:05.267 回答
0

首先,更新你的数据模型(NSArray 或可变的)。二、当要刷新tableview时,添加代码[self.tableView reloadData];


哎呀,你的代码有些奇怪的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 下方。请参考。


案例2。有数据模型。

- (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;
}
于 2012-04-02T03:26:52.667 回答
0

您只需要将新部分和新项目添加到包含该部分和项目的数组中,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

[self.tableView reloadData];
于 2015-11-07T22:58:24.047 回答