0

ユーザーがいくつかのアイテムに対して複数の選択肢を選択できるようにする必要があります。この目的のために、私は以下を使用しています。

唯一の問題は、accessoryView を削除するために行を 2 回選択したことです。最初の選択で行を選択できます。ただし、行を選択した後、タッチして選択を解除すると、最初にハイライト色のみが表示され、もう一度クリックしてその行の選択を解除する必要があります。

なぜこれが起こっているのですか?最初のタッチで行の選択を解除したい。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
int i=indexPath.row;
if(self.multi == 1){

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"accessoryType:%d",selectedCell.accessoryType);
    if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;

        NSString *cellText = selectedCell.textLabel.text;
        [self.selectedValues replaceObjectAtIndex:i withObject:cellText];
    }else{
        selectedCell.accessoryType = UITableViewCellAccessoryNone;                 
         [self.selectedValues replaceObjectAtIndex:i withObject:@""];         
    }        
}
else{

    NSLog(@"not for multi select",nil);
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = selectedCell.textLabel.text;
    [self.delegate addItemViewController:self setPeculiarity:cellText setIndex:self.index];

    [self dismissModalViewControllerAnimated:YES];
}
}

編集 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"Cell";    

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}
    cell.textLabel.text = [pecs objectAtIndex:indexPath.row];

if(self.selectedValues.count > 0){
    for(int k = 0 ; k < [selectedValues count];k++){

        if ([[self.selectedValues objectAtIndex:k] isEqualToString:[pecs objectAtIndex:indexPath.row]]) {

            cell.accessoryType = UITableViewCellAccessoryCheckmark;
              NSLog(@"pecs:%@",[pecs objectAtIndex:indexPath.row]);
        }
    }
}
// for background color
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.2823 green:0.4509 blue:0.8588 alpha:1.0];
[cell setSelectedBackgroundView:bgColorView];

cell.textLabel.highlightedTextColor = [UIColor whiteColor];
return cell;
}
4

2 に答える 2

1

[tableView reloadData]全体を使用する場合はtableViewリロードされます。

シミュレーターではメモリと実行時間はそれほどかかりませんが、実際の iPhone / iPad / iPod ではより多くのリソースが必要になります。

[tableView reloadData]と 置き換えます[tableView reloadRowsAtIndexPaths: [NSArray arrayWithObjects: [NSIndexPath indexPathForRow: indexPath.row inSection: indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];

UITableViewRowAnimationNoneあなたはと置き換えることができます

UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
于 2013-10-17T06:00:19.400 に答える