0

这个问题很简单。对于在第 0 节和第 1 节中具有相同标题的每一行,如何使 tableView 中的附件视图被“选中”?(iOS)

我想这样做是因为当我连续选择附件视图时,该行会复制到一个新的第 0 部分,该部分称为“收藏夹”。但问题是,如果我取消选择第 0 部分中的行,该行会消失,但在第 1 部分中的相应行中,附件视图仍处于选中状态,在这种情况下我不想被取消选择。提前致谢。

-(void)addToFavs:(id)sender event:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    UIButton *button = (UIButton*)sender;
    if(indexPath!=nil){
            if(button.selected==YES){
                button.selected = NO;
            }else{
                button.selected =YES;
            }
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell){
        // left image
        UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.textColor=[UIColor lightGrayColor];
        cell.textLabel.text=self.objects[indexPath.row];
        cell.detailTextLabel.text =self.subtitles[indexPath.row];
        [cell.contentView addSubview:image];

        if(indexPath.section==0){
            image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
            cell.textLabel.text=self.favs[indexPath.row];
            cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
        }else{
            image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
        }
        //favorites image button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0.0, 0.0, 25, 25);
        button.frame = frame;
        button.showsTouchWhenHighlighted = YES;
        [button setImage:[UIImage imageNamed:@"unfavorites.png"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"favorites.png"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(addToFavs:event:)  forControlEvents:UIControlEventTouchUpInside];
        if(indexPath.section==0){
            button.selected = !button.selected;
        }
        cell.accessoryView.tag=indexPath.row;
        button.backgroundColor = [UIColor clearColor];
        cell.accessoryView = button;
    }
    return cell;
}
4

1 回答 1

0

简单的方法是,为收藏创建一个数组并在选择时将项目添加到该数组中。如果该项目在收藏夹中,则选择附件视图。

于 2014-01-14T12:32:12.130 回答