如何删除最后一行并在tableview中添加更多行?

时间:2012-03-18 13:01:11

标签: iphone ios xcode uitableview

在tableview的最后一行有一个按钮,当按下它时,我希望该行中的按钮消失,同时添加更多行,然后,按钮再次出现在最后一行。 但我不知道怎么做! 这是我的代码:

- (void)morePicture:(id)sender{
NSMutableArray *indexpath = [[NSMutableArray alloc] init];
[photos removeObjectAtIndex:[photos count]-1];
[indexpath addObject:[NSIndexPath indexPathForRow:[photos count]+1 inSection:0]];
[table beginUpdates];
[table deleteRowsAtIndexPaths:indexpath withRowAnimation:UITableViewRowAnimationNone];

NSMutableArray *indexPathss = [[NSMutableArray alloc] init];
for(int i=0; i<3; i++){
    NSString *s = [[NSString alloc] initWithFormat:@"%d",i];
    [photos addObject:s];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
    [indexPathss addObject:indexpath];
}

2 个答案:

答案 0 :(得分:1)

您无需调用deleteRowsAtIndexPaths方法。请改用insertRowsAtIndexPaths。只需在最后一行按钮上方添加行,不要删除它!

NSArray *newPhotos = ....;
NSMutableArray *photos = ...;

// insert new photos first (before table update)
[photos insertObjects:newPhotos atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([photos count] - 1, [newPhotos count])];

NSMutableArray *newIndices = [NSMutableArray array];
for (NSInteger row = [photos count] - [newPhotos count] - 1; row < [photos count] - 1; row++) {
    [newIndices addObject:[NSIndexPath indexPathWithIndex:row];
}
[self.tableView insertRowsAtIndexPaths:newIndices withRowAnimation:UITableViewRowAnimationFade];

答案 1 :(得分:0)

UIButton * moreButton = [[UIButton alloc] initWithFrame:frame];

[moreButton setImage:[UIImage imageNamed:@“button_signin.png”] forState:UIControlStateNormal];

[moreButton addTarget:self action:@selector(showMoreRows)forControlEvents:UIControlEventTouchUpInside];

//向表格页脚视图添加按钮

self.tableView.tableFooterView = moreButton;

方法

- (IBAction)showMoreRows {

//在数组中添加对象,该对象负责创建表视图,然后调用

[self.tableView reloadData];

}