如何在表格中插入行?

时间:2022-09-25 21:24:03

I'm new in objective-c. I have a tableView with 3 rows and 1 section. Can you help me how to add row with some text in a table by pressing button (addCity)?

我是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 个解决方案

#1


14  

Your table must take its data from some source where you can add elements when needed. (say, NSMutableArray instance in your data source), then your methods will look like. For simplicity assume that you have dataArray containing NSStrings:

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

#2


2  

You don't insert rows into the tableView directly. You adopted the UITableViewDataSource protocol. Your data source provides the table-view object with the information it needs to construct and modify a table view. UITableViewDataSource Protocol Reference

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

Also, looking at your sample code, you hard coded the number of rows in your table to 3. Therefore, UITableView is only going to display 3 rows. You need something like this:

另外,查看示例代码,您将表中的行数硬编码为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];
}

#1


14  

Your table must take its data from some source where you can add elements when needed. (say, NSMutableArray instance in your data source), then your methods will look like. For simplicity assume that you have dataArray containing NSStrings:

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

#2


2  

You don't insert rows into the tableView directly. You adopted the UITableViewDataSource protocol. Your data source provides the table-view object with the information it needs to construct and modify a table view. UITableViewDataSource Protocol Reference

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

Also, looking at your sample code, you hard coded the number of rows in your table to 3. Therefore, UITableView is only going to display 3 rows. You need something like this:

另外,查看示例代码,您将表中的行数硬编码为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];
}