【问题标题】:How can I insert row in a table?如何在表格中插入行?
【发布时间】:2011-03-25 08:09:04
【问题描述】:

我是 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];
}

【问题讨论】:

  • 我更新了我的代码。它不起作用。
  • 我已删除 insertRowsAtIndexPaths 并且我的程序正在运行!

标签: objective-c uitableview insert row


【解决方案1】:

您的表格必须从某个来源获取数据,您可以在需要时添加元素。 (例如,您的数据源中的 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];
}

【讨论】:

  • 类似的东西? dataArray = [[NSMutableArray alloc] initWithObjects:@"Moscow", @"London", @"Paris", nil];我的程序崩溃了。
  • @Sveta,是的,数组应该以这种方式创建......你还有崩溃吗?
  • 不,我已删除 insertRowsAtIndexPaths 并且我的程序正在运行!
  • 那么您可能在 dataArray 的 addObject: 方法中做了一些 reloadData。因为弗拉基米尔是对的,所以应该这样做,用insertRowsAtIndexPaths:
  • insertRow 代码在点击按钮时工作正常,但在 viewDidLoad 方法上无法正常工作
【解决方案2】:

您不会直接将行插入到 tableView 中。您采用了 UITableViewDataSource 协议。您的数据源为 table-view 对象提供了构建和修改 table view 所需的信息。 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];
}

【讨论】:

  • 我认为他想插入单独的行并有一个漂亮的动画,你的方法没有提供一个漂亮的动画。
猜你喜欢
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 2023-03-22
  • 2020-08-24
  • 2018-10-14
  • 2011-03-05
相关资源
最近更新 更多