1

次のコードを使用して、カスタム アイコンを UITableViewCellAccessoryCheckmark に追加しています。問題は、別の行がタッチ/選択されたときにセルのチェックマークが選択解除されないことです

ここに私のコードがあります:

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

    int index = indexPath.row; id obj = [listOfItems objectAtIndex:index];


    //This toggles the checkmark
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];


    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [downloadButton setImage:image forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 19, 19)];
        [downloadButton setBackgroundColor:[UIColor clearColor]];
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

        //This sets the array

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [downloadButton setTitle:@"" forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 0, 0)];
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

    }

    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // Customize archiver here
    [archiver encodeObject:obj forKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [archiver finishEncoding];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"keyForYourArrayOfNSIndexPathObjects"];


    NSKeyedUnarchiver *unarchiver;
    unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
                  [[NSUserDefaults standardUserDefaults] objectForKey:@"keyForYourArrayOfNSIndexPathObjects"]];
    // Customize unarchiver here
    countryItemSelected = [unarchiver decodeObjectForKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [unarchiver finishDecoding];

    NSLog(@"list of items %@", countryItemSelected);

    // Navigation logic may go here. Create and push another view controller.

     CitiesViewController *detailViewController = [[CitiesViewController alloc] init];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];

}

助けてくれてありがとう

4

3 に答える 3

2

「チェックマークが選択解除されない」とはどういう意味ですか?とにかく:tableView:didDeselectRowAtIndexPath:セルが選択解除されたときに、デリゲートメソッドを使用して、アクセサリを別のものに変更できます。

于 2013-01-17T11:40:57.153 に答える
0

以下のコードを試してください

int rowNumber = [tableCategory numberOfRowsInSection:0];

for (int indexRow = 0 ; indexRow < rowNumber; indexRow++)
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexRow inSection:0];

    UITableViewCell *cell = [tableCategory cellForRowAtIndexPath:indexPath];

    if (indexPath.row == <required indexpath row>)
    {
        //do what you want with cell
    }
    else
    {
        //do what you want with cell
    }
}

それがあなたを助けることを願っています

于 2013-01-17T12:07:19.687 に答える