1

セルのaccessoryViewに画像を含むuiviewを設定しましたが、後でこのビューを削除して、accessoryTypeをnoneとして再度表示できるようにします。以下は機能しません -

  //create cell
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

        //initialize double tick image
        UIImageView *dtick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dtick.png"]];
        [dtick setFrame:CGRectMake(0,0,20,20)];
        UIView * cellView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
        [cellView addSubview:dtick];

 //set accessory type/view of cell
        if (newCell.accessoryType == UITableViewCellAccessoryNone) {
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        else if(newCell.accessoryType == UITableViewCellAccessoryCheckmark){
                newCell.accessoryType = UITableViewCellAccessoryNone;
                newCell.accessoryView = cellView;
            }
        else if (newCell.accessoryView == cellView) {
            newCell.accessoryView = nil;
            newCell.accessoryType = UITableViewCellAccessoryNone;
          }

[newCell.accessoryView reloadInputViews] も試しましたが、どちらも機能しません。

基本的に、セルをクリックするとこれらの状態を循環させたい=>ティックなし->1つのティック->ダブルティック(画像)->ティックなし

どんな助けでも大歓迎です、ありがとう。

4

1 に答える 1

5

There are two problems with your code:

  • In newCell.accessoryView == cellView you compare the cell's accessory view with a newly created image view: This comparison will never yield TRUE.

  • When you set the accessory view to your image, you also set the type to UITableViewCellAccessoryNone, so that the next time it will be set to UITableViewCellAccessoryCheckmark again. In other words, the second else if block will never be executed.

The following code could work (but I did not try it myself):

if (newCell.accessoryView != nil) {
     // image --> none
     newCell.accessoryView = nil;
     newCell.accessoryType = UITableViewCellAccessoryNone;
} else if (newCell.accessoryType == UITableViewCellAccessoryNone) {
     // none --> checkmark
     newCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (newCell.accessoryType == UITableViewCellAccessoryCheckmark) {
     // checkmark --> image (the type is ignore as soon as a accessory view is set)
     newCell.accessoryView = cellView;
}
于 2012-07-27T05:14:28.237 に答える