2015-09-30 2 views
2

Я работаю над множественным выбором tableview, снимите выделение.UITableViewCellAccessoryCheckmark исчезнет после прокрутки стола.

У меня 3 вида таблицы (Tableview которые имеют тег 400, мне нужно множественный выбор и отмену выбора -> alllMumbaiTableview)

Мой вопрос: Когда я выбрать несколько строк, UITableViewCellAccessoryCheckmark прибудет исчез после прокрутки таблицы. Но вот здесь я вижу, что эти ячейки все еще находятся в только в выбранном состоянии, но не отображаются UITableViewCellAccessoryCheckmark.

enter image description here

После прокрутки мой стол выглядит как этот

enter image description here

Вот мой код

@property (nonatomic, strong) NSMutableArray *arrIndexpaths; 

UITableView M ethods -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     static NSString *simpleTableIdentifier = @"SimpleTableItem"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 


     if(tableView.tag == 100) 
     { 
       cell.textLabel.text = [typeArray objectAtIndex:indexPath.row]; 
     } 
     else if (tableView.tag == 300) 
     { 
       cell.textLabel.text = [specialityArray objectAtIndex:indexPath.row]; 
     } 
     else if (tableView.tag == 400) 
     { 
       if (cell == nil) 
       { 

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 

       } 
       if ([self.arrIndexpaths containsObject:[NSIndexSet indexSetWithIndex:indexPath.row]]) 
       { 
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
       } 
       else 
       { 
        cell.accessoryType = UITableViewCellAccessoryNone; 
       } 

        cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row]; 

     } 
    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 


     if(tableView.tag == 100) 
     { 
      _typeTextfield.text=[typeArray objectAtIndex:indexPath.row]; 
      _typeTableview.hidden = YES; 
     } 
     else if (tableView.tag == 300) 
     { 
      _specialityTextield.text=[specialityArray objectAtIndex:indexPath.row]; 
      _specialityTableview.hidden = YES; 
     } 
     else if (tableView.tag == 400) 
     { 

      UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
      tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 
      [self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]]; 
     } 

} 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    if (tableView.tag == 400) 
    { 
     UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
     tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
     [self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]]; 

    } 

    } 

Что я хочу:

1) Я хочу UITableViewCellAccessoryCheckmark даже после прокрутки TableView (Но не получить выбранный другие строки, когда я выбирая один. Моя предыдущая проблема ->Selecting one UITableViewCell selects the other cell after scrolling)

2) Мне нужны все только выбранные значения в один массив.

ответ

0

я должен использовать другой идентификатор для каждой таблицы & возврата ячейки для каждого Tableview, затем он работал для меня

Обновленный код

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     if(tableView.tag == 100) 
    { 

     static NSString *simpleTableIdentifier = @"SimpleTableItem1"; 

     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
     } 


     cell.textLabel.text = [typeArray objectAtIndex:indexPath.row]; 

     return cell; 

    } 
    else if (tableView.tag == 300) 
    { 

     static NSString *simpleTableIdentifier = @"SimpleTableItem2"; 

     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
     } 


     cell.textLabel.text = [specialityArray objectAtIndex:indexPath.row]; 

     return cell; 


    } 
    else 
    { 

      static NSString *simpleTableIdentifier = @"SimpleTableItem3"; 

      UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 

      if (cell == nil) 
      { 

       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 

      } 

      if ([self.arrIndexpaths containsObject:[NSIndexSet indexSetWithIndex:indexPath.row]]) 
      { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      else 
      { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } 

      cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row]; 

      return cell; 

     } 



} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    if(tableView.tag == 100) 
    { 
    _typeTextfield.text=[typeArray objectAtIndex:indexPath.row]; 
    _typeTableview.hidden = YES; 
    } 
    else if (tableView.tag == 300) 
    { 
     _specialityTextield.text=[specialityArray objectAtIndex:indexPath.row]; 
     _specialityTableview.hidden = YES; 
    } 
    else if (tableView.tag == 400) 
    { 


     UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
     tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     [self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]]; 
    } 

} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    if (tableView.tag == 400) 
    { 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
    [self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]]; 

    } 

} 
2

Я хотел бы предложить иметь каждый различный идентификатор ячейки. Я не знаю, как установить раскладку tableviews, но выглядит, что очередь tableviewcell заменяет другие ячейки, в отличие от вышеперечисленных.

static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

if(tableView.tag == 100) 
{ 
    simpleTableIdentifier = @"SimpleTableItem"; 
} 
else if (tableView.tag == 300) 
{ 
    simpleTableIdentifier = @"SimpleTableItem1"; 
} 
else if (tableView.tag == 400) { 

    simpleTableIdentifier = @"SimpleTableItem2"; 
} 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

Update окончательного

#import "ViewController.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> { 
    NSArray *tmpArray; 
} 

@property(nonatomic, weak) IBOutlet UITableView *tableView1, *tableView2, *tableView3; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    tmpArray = @[[NSNumber numberWithInt:2],[NSNumber numberWithInt:3]]; 

    [self.view bringSubviewToFront:self.tableView1]; 
    [self.view bringSubviewToFront:self.tableView2]; 
    [self.view bringSubviewToFront:self.tableView3]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 20; 
} 

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    if (self.tableView1 == tableView) 
    { 
     simpleTableIdentifier = @"SimpleTableItem"; 
    } 
    else if (self.tableView2 == tableView) 
    { 
     simpleTableIdentifier = @"SimpleTableItem1"; 
    } 
    else if (self.tableView3 == tableView) { 

     simpleTableIdentifier = @"SimpleTableItem2"; 
    } 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 
    } 

    if (self.tableView1 == tableView) 
    { 
     cell.textLabel.text = @"tableView1"; 
    } 
    else if (self.tableView2 == tableView) 
    { 
     cell.textLabel.text = @"tableView2"; 
    } 
    else if (self.tableView3 == tableView) { 

     if ([tmpArray objectAtIndex:0] == [NSNumber numberWithInt:(int)indexPath.row] || 
      [tmpArray objectAtIndex:1] == [NSNumber numberWithInt:(int)indexPath.row]) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } else { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 

     cell.textLabel.text = @"tableView3"; 
    } 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 70; 
} 
+0

Я не получаю .. может у пожалуйста, обновить ваш код в отношении моего кода? –

+0

обновил мой код, как и сказал, но все равно столкнулся с тем же –

+0

Вы проверили, что if ([self.arrIndexpaths containsObject: [NSIndexSet indexSetWithIndex: indexPath.row]]) отлично подходит? – jamesBlake

0

Есть много, чтобы решить данную проблему.

Но самым простым способом является то, что не используйте повторно ячейку в cellforrowatindexpath.

создать новый экземпляр ячейки каждый раз & Надеюсь, что вы оба решите проблему.

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 

Я надеюсь, что это сработает для вас, его работы для меня.

0

Я сделал то же самое, используя кнопку и меняя ее изображение на Selection/Deselection, ниже приведен код, который может вам помочь.

cell.btnCheckMark.tag = indexPath.ряд;

[cell.btnCheckMark addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    NSString *str1 = [[NSString alloc]init]; 
    str1 = [NSString stringWithFormat:@"%d",cell.btnCheckMark.tag]; 


    //str2 = [NSString stringWithFormat:@"%@",cell.lblText.text]; 

    if ([arrSelect containsObject:str1]) { 
     cell.btnCheckMark.selected=YES; 
     str2 = [NSString stringWithFormat:@"%@",cell.lblText.text]; 
    }else{ 
     cell.btnCheckMark.selected=NO; 
    } 

- (Недействительными) buttonPressed: (идентификатор) отправитель {

UIButton *btn = (UIButton *)sender; 

btn.selected=!btn.selected; 

str= [NSString stringWithFormat:@"%d",btn.tag]; 
displayValues = [NSString stringWithFormat:@"%@",[arrName objectAtIndex:[str integerValue]]]; 

if (btn.selected) { 
    [arrSelect addObject:[NSString stringWithString:str]]; 
    // NSLog(@"Name: %@",[arrName objectAtIndex:[str integerValue]]); 
} 
else{ 
    [arrSelect removeObject:[NSString stringWithString:str]]; 
    // NSLog(@"%@",[arrName objectAtIndex:[str integerValue]]); 

} 

}

0

Shrikant

я думаю, что это происходит из-за многократного использования клетки.

вы даете попробовать

Заменяйте

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

с

UITableViewCell *cell = nil; 

я думаю, что это будет работать нормально.

0

Я обновляю ваш код.

В ViewDidiLoad ме-

if (self.arrIndexpaths) { 
    self.arrIndexpaths = [[NSMutableArray alloc] init]; 
} 

Первое обновление Метод cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *simpleTableIdentifier = @"SimpleTableItem"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];//Update 


    if(tableView.tag == 100) 
    { 
      cell.textLabel.text = [typeArray objectAtIndex:indexPath.row]; 
    } 
    else if (tableView.tag == 300) 
    { 
      cell.textLabel.text = [specialityArray objectAtIndex:indexPath.row]; 
    } 
    else if (tableView.tag == 400) 
    { 
      /* if (cell == nil) 
      { 

       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 

      }*///Update 
      if ([self.arrIndexpaths containsObject:indexPath])//Update 
      { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      else 
      { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } 

       cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row]; 

    } 
return cell;} 

Второе обновление DidSelectRowIndexPath Метод

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(tableView.tag == 100) 
    { 
     _typeTextfield.text=[typeArray objectAtIndex:indexPath.row]; 
     _typeTableview.hidden = YES; 
    } 
    else if (tableView.tag == 300) 
    { 
     _specialityTextield.text=[specialityArray objectAtIndex:indexPath.row]; 
     _specialityTableview.hidden = YES; 
    } 
    else if (tableView.tag == 400) 
    { 

     UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];//Update 
      if ([self.arrIndexpaths containsObject:indexPath])//Update 
      { 
      tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
      [self.arrIndexpaths removeObject:indexPath]; 
      } 
     else 
      { 
      tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 
      [self.arrIndexpaths addObject:indexPath]; 
      } 
     } 
     } 

Надежда выше код помощь для Вас.

Смежные вопросы