1

UIViewControllerの には、セルがテキストを表示するテーブル ビューがあります。のデータソースとデリゲート UITableViewは myUIViewControllerです。セルによって表示されるテキストは、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

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