0

私は次のことをしたいと思います: テーブルビューの行をクリックした後、いくつかの変数の値を設定し、同時にセルのアクセサリの種類をチェックマークに変更したいと思います。ここに私の実装ファイルがあります:

- (void)viewDidLoad
{
    [super viewDidLoad];
    tabledata = [[NSArray alloc] initWithObjects:@"row1", @"row2", @"row3", nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableView Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tabledata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"Row"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Row"];
    }

    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark TableVoew Delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if row1 is selected change 'value' to 1 and change cell accessory to checkmark (other cells accessory to none)
//if row2 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
//if row3 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
}
4

2 に答える 2

0

これにより、古いチェックマークを削除する際の問題が解決しました。

for (UITableViewCell *cell in [tableView visibleCells]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

現在の行の値の設定に関する問題は、前の回答で解決されました。

于 2012-10-24T14:31:56.323 に答える
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.value = indexPath.row;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

編集 選択した行のアクセサリのみを設定します- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

cell.accessoryType = UITableViewAccessoryNone;

if (indexPath.row == self.value) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

テーブルビューで現在表示されているセルからチェックマークを削除するには、これを参照してください。

EDIT 2 前の行の選択を解除するには、これをdidSelectRowAtIndexPath

NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:index inSection:1];
NSLog(@"Oldindexpath is: %@", oldIndexPath);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

if (newCell.accessoryType == UITableViewCellAccessoryNone) {
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];

if (oldCell != newCell){
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    } 
}
于 2012-10-24T09:29:42.867 に答える