为指定的单元格选择accessoryView

时间:2014-01-14 11:03:41

标签: ios objective-c uitableview selected accessoryview

问题很简单。如何使tableView中的accessoryView对于0和1中具有相同标题的每一行都“选中”? (IOS)

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

-(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;
}

1 个答案:

答案 0 :(得分:0)

这样做很简单,为收藏夹创建一个数组,并在select数据库中添加项目。如果项目在收藏夹数组中,请选择附件视图。