1

在我的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];
    }
}
4

5 回答 5

2

最少的代码方式是:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self.modelArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

这些仅需要将多个更改一起批处理:

[tableView beginUpdates];
[tableView endUpdates];
于 2013-09-05T09:36:36.120 回答
1

您可以使用didSelectRowAtindexPathtableview的方法

- (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];
}
于 2013-09-05T09:20:23.703 回答
1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [YourArr removeObjectAtIndex:indexPath.row];
    [YourTbl reloadData];
}
于 2013-09-05T09:20:59.733 回答
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];


}

编辑:似乎每个人都有相同的答案!

于 2013-09-05T09:22:57.913 回答
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];
}
于 2013-09-05T09:17:26.400 回答