在我的UIViewController
中,我有一个表格视图,其单元格显示文字。 UITableView
的数据源和代理是我的UIViewController
。单元格显示的文本存储在NSMutableArray
。
我最初可以使用文本填充单元格,但我还想实现一项功能,当用户点击特定行时,单元格将被删除并重新加载表格。
这是我的代码及其详细信息:
taskList is a NSMutableArray
png images are ones that i need to add in every cell
my table contains only one section
the last row of the table will always show "Create New Task" with a "+" sign as an image
as user adds other task (handled by a separate controller), the taask gets added to the current table.The task will have a "-" sign image.
When user clicks on such a row, it will get deleted from the table
UIViewController的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
taskList = [[NSMutableArray alloc] init];
addNewTask = [[AddTaskViewController alloc] init];
[taskList addObject:@"Create New Task"];
[taskList addObject:@"aaaaa"];
[taskList addObject:@"bbbbb"];
[taskList addObject:@"ccccc"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
indexNumberInTaskList = [taskList count] - 1;
return [taskList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
}
if(indexNumberInTaskList >= 0)
{
cell.textLabel.text = [taskList objectAtIndex:indexNumberInTaskList];
if(indexPath.row == ([taskList count] - 1))
{
cell.imageView.image = [UIImage imageNamed:@"add.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:@"remove.png"];
}
indexNumberInTaskList = indexNumberInTaskList - 1;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int selectedRow = indexPath.row;
if(selectedRow == ([taskList count]-1))
{
[self presentViewController:addNewTask animated:YES completion:NULL];
}
else
{
[taskList removeObjectAtIndex:indexPath.row];
[self.createDaily reloadData];
}
}
答案 0 :(得分:2)
最少的代码方式是:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.modelArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
只需将多个更改一起批处理即可:
[tableView beginUpdates];
[tableView endUpdates];
答案 1 :(得分:1)
您可以使用tableview的didSelectRowAtindexPath
方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayData removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow: indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
}
答案 2 :(得分:1)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourArr removeObjectAtIndex:indexPath.row];
[YourTbl reloadData];
}
答案 3 :(得分:1)
根据您的-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
委托方法
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
NSArray *toBeDeleted = [[NSArray alloc] initWithObjects: indexPath];
// Data deletion
[tableData removeObjectAtIndex:(indexPath.row)];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:toBeDeleted withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
编辑:似乎每个人都有相同的答案!
答案 4 :(得分:0)
添加此方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrData removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow: indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
}