首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在表格中插入行?

如何在表格中插入行?
EN

Stack Overflow用户
提问于 2011-03-25 16:09:05
回答 2查看 29.9K关注 0票数 8

我是objective-c的新手。我有一个3行1节的tableView。您能告诉我如何通过按按钮(addCity)在表格中添加带有一些文本的行吗?

代码语言:javascript
运行
复制
- (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];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-25 16:18:37

您的表必须从某些源获取数据,您可以在需要时添加元素。(比方说,数据源中的NSMutableArray实例),那么您的方法将如下所示。为简单起见,假设您有包含NSStrings的dataArray:

代码语言:javascript
运行
复制
- (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];
}
票数 15
EN

Stack Overflow用户

发布于 2011-03-25 16:27:15

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

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

代码语言:javascript
运行
复制
// 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];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5429960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档