objective-c - 如何在表格中插入行?

标签 objective-c uitableview insert row

我是 objective-c 的新手。我有一个包含 3 行和 1 个部分的 tableView。你能帮我如何通过按下按钮 (addCity) 在表格中添加带有一些文本的行吗?

- (void)viewDidLoad {
    [super viewDidLoad];
    m_bg.image = [UIImage imageNamed:[WeatherAppDelegate isNight]  ? @"SettingsBackNight.png" : @"SettingsBackDay.png" ];
    tableView.layer.cornerRadius = 10;
    m_scrollView.layer.cornerRadius = 10;
    [m_scrollView setContentSize:CGSizeMake(tableView.frame.size.width, tableView.frame.size.height)];
    dataArray = [[NSMutableArray alloc] initWithObjects:@"Moscow", @"London", @"Paris", nil];

}


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


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }

    [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row]];
    return cell;
}


-(IBAction)addCity:(id)sender
{
    [dataArray addObject:@"City"];
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
    [tableView reloadData];
}

最佳答案

您的表格必须从某个来源获取数据,您可以在需要时从中添加元素。 (例如,数据源中的 NSMutableArray 实例),那么您的方法将看起来像。为简单起见,假设您有包含 NSString 的 dataArray:

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

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *MyIdentifier = @"MyIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
  }
  [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row];
  return cell;
}


-(IBAction)addCity:(id)sender
{
  [tableView beginUpdates];
  [dataArray addObject:@"City"];
  NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
  [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
  [tableView endUpdates];
}

关于objective-c - 如何在表格中插入行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5429960/

相关文章:

php - 从一个表单插入到两个表中

python - 如何使用 Python 向 Oracle 数据库中插入 100 万行?

objective-c - Parsekit 自动补全

ios - 初始化后不显示带有 Nib 的 UITableViewCell

ios - 在 Objective-C 中找不到带有 Swift 类参数的方法

Swift SQLite 数据库如何插入日期字段

objective-c - iOS:将类分配给xib,但无法识别

ios - Swift UITableView,插入,然后删除,然后插入

iphone - 在 UITableView 中的当前行顶部插入行

ios - (iPhone)如何在TableView中仅更新一行