Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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
Iphone 如何动态添加节和表视图单元格_Iphone_Objective C_Ios_Tableview_Tableviewcell - Fatal编程技术网

Iphone 如何动态添加节和表视图单元格

Iphone 如何动态添加节和表视图单元格,iphone,objective-c,ios,tableview,tableviewcell,Iphone,Objective C,Ios,Tableview,Tableviewcell,我有一个按钮,当按下时,一个tableview部分将被添加,也将添加一个新行到该部分。我如何通过编程实现这一点 我有一个细胞阵列 这是我的密码 - (IBAction)addCell:(id)sender { UITableViewCell *newCell = [[UITableViewCell alloc] init]; counter++; [cells addObject:newCell]; [self.tableView reloadData];

我有一个按钮,当按下时,一个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];
}
您好,请尝试阅读以下内容:



希望这能对您有所帮助。

首先,更新您的数据模型(NSArray或mutable)。第二,当需要刷新tableview时,添加代码[self.tableview reloadData]


哦,你的代码有一些奇怪的uitaleviewDataSource,委托方法

还有,你的电脑有一些错误

为什么要在返回1部分中实现numberOfRowsInSection?很奇怪

我知道下面的代码

案例1。没有数据模型

- (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;
   }
- (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;
}
您的代码、数据模型(NSArray或mutable)不一定需要,所以我只添加了rowCount变量,并且您的tableviewCell的rowsCount已经同步

如果您想使用DataModel,请参见下面的案例2。参考plz


案例2。具有数据模型

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

只需将新节和新项添加到保存节和项的数组中,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    animals = @{@"Section 1" : @[@"Item 1", @"Item 2", @"Item 3"],
                @"Section 2" : @[@"Item 1", @"Item 2"]};
}
之后,只要在逻辑需要的地方重新加载tableView即可:

[self.tableView reloadData];

谢谢,但是我在将单元格动态添加到tableview中没有问题。我关心的是如何将节和单元格添加到新添加的节中。正如我们所知,我们必须配置,-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView方法来返回节数。我有一个计数器可以在按下按钮后重新加载表格。问题是如何处理-(UITableViewCell*)tableView:(UITableView*)tableView CellForRowatineXpath:(NSIndexPath*)indexPath方法,该方法控制每个节的if语句的显示?请建议注释中的链接,而不是作为答案。问题是如何处理-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath方法,用于控制要显示的每个节的if语句?Multisection是mean equal 2d数组模型,如果要扩展节,则数据模型必须实现2d数组。另外关于tableView numberOfSectionInTableview方法添加代码返回[myArray count];也实现。numberOfSection实现返回[myArr计数];numberOfRowsInSection必须实现返回[[myArr objectAtIndex:section]计数];还是不明白。我用代码更新了我的问题。请看你是否清楚这一点。非常感谢。您所做的不是处理
UITableView
s的好方法!您已在其他位置创建了
UITableViewCell
s,但建议在
cellforrowatinexpath
中创建。只需更改
数据源
,然后重新加载表,即可完成所需操作。