2014-01-14 2 views
0

вопрос довольно прост. Как вы можете сделать accessView в tableView «выбранным» для каждой строки, которая имеет один и тот же заголовок в обоих разделах 0 и 1? (iOS)выбранные принадлежностиПросмотрите для указанных ячеек

Я хочу сделать это, потому что, когда я выбираю аксессуар в строке, строка копирует до новой секции 0, которая называется «избранное». Но проблема в том, что, если я отменил выделение строки в разделе 0, строка исчезнет, ​​но элемент accessView останется выбранным для соответствующей строки в разделе 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; 
} 

ответ

0

Простой способ сделать это, создать массив для избранных и добавить элемент в этот массив при выборе. И выберите аксессуар, если элемент находится в списке избранных.

+0

Да, я просто получил работу с этим 'if ([self.favs containsObject: [self.objects objectAtIndex: indexPath.row]]) { button.tag = button.selected = YES; } else { button.tag = button.selected = NO; } } ' , но он не обновляется напрямую, как я могу это исправить? – Tibbe

+0

вы перезаряжаете камеру после этого ...? лучший способ будет заключаться в том, что когда вы продолжаете следить за ячейкой, вы должны перезагрузить ее ... вы можете сделать это с помощью [tableView reloadRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationAutomatic]; –

+0

Я положил его в блок if (cell) в cellForRowAtIndexPath – Tibbe