如何在表格中插入行?

时间:2011-03-25 08:09:05

标签: 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];
}

2 个答案:

答案 0 :(得分:15)

您的表必须从某些来源获取数据,您可以在需要时添加元素。 (比如说,数据源中的NSMutableArray实例),那么你的方法就会像。为简单起见,假设您有包含NSStrings的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];
}

答案 1 :(得分:2)

您不直接在tableView中插入行。您采用了UITableViewDataSource协议。您的数据源为表视图对象提供构建和修改表视图所需的信息。 UITableViewDataSource Protocol Reference

另外,查看示例代码,您将表中的行数硬编码为3.因此,UITableView仅显示3行。你需要这样的东西:

// You have a city array you created
NSMutableArray *cityArray = ......

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

-(IBAction)addCity:(id)sender
{
   // create a new city object
   // add the object to your array
   [cityArray addObject:....
   // refresh the table
   [tableView reloadData];
}
相关问题