选定的附件查看指定单元格



这个问题很简单。如何使表视图中的附件视图对于在第 0 节和第 1 节中具有相同标题的每一行都"选定"?(苹果)

我想这样做是因为当我在一行中选择附件视图时,该行最多复制到称为"收藏夹"的新部分 0。但问题是,如果我取消选择第 0 节中的行,该行会消失,但 accessoryView 会为第 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;
}

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

最新更新