如何在表格中插入行?
问题描述:
我是新的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];
}
答
您不直接将行插入到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];
}
+0
我认为他想插入单独的行,并有一个很好的动画,您的方法不提供一个很好的动画。 – 2016-01-13 05:53:47
我已更新我的代码。它不起作用。 – Sveta 2011-03-25 08:38:52
我被删除insertRowsAtIndexPaths和我的程序正在工作! – Sveta 2011-03-25 08:56:04