0

我的 viewController 包含一个 tableView,用户可以在其中通过 coreData 添加单元格。我定义了单元格:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Travel";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];

        UILabel *countryLabel = (UILabel *)[cell viewWithTag:101];
        kategorieLabel.text = [travel valueForKey:@"country"];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
        [cell addSubview:button];

            if ([[travel valueForKey:@"tick"] isEqualToString:@"yes"]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
        return cell; 
    }

- (void)buttonTapped:(UIButton *)sender {
    UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
    [owningCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    NSIndexPath *indexPath = [_tableView indexPathForCell:owningCell];
    NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
    [travel setValue:@"yes" forKey:@"tick"];
    }

当用户点击单元格上的按钮时,应出现复选标记。但是当我重新加载或重新启动应用程序时,并不是所有的复选标记都在那里。也许有更好的方法来做到这一点。

4

0 回答 0