2013-10-03 2 views
-1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

} 
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]]; 
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
if(getIndex<21) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
cell.textLabel.text = [myArray objectAtIndex:indexPath.row]; 

return cell; 
} 

это мой код, когда когда-либо я начал прокрутку таблицы в направлении вниз или вверх первая ячейка, которые я проверил бы освобожденный знак галочки, он просто скрывает все Кстати .. что случилось с моим кодом?Первая галочка скрывает, когда я начал прокрутку таблицы

ответ

2

Повторное использование клеток,

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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


     int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     if(getIndex<21) 
     { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } // END HERE 
    cell.textLabel.text = [myArray objectAtIndex:indexPath.row]; 
    return cell; 
} 
+0

, применяя ваш код, он автоматически выбирает некоторые другие ячейки тоже – Steve

+0

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

+0

. Ваш способ настройки getIndex в этом случае должен быть неправильным. Попробуйте отладить и получить правильный индекс. Альтернативно, первые 10 строк идут с indexPath.row –

2

Для выбора нескольких строк:

- (void)addTableView { 

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set") 
                  style:UITableViewStylePlain]; 
self.tableView.dataSource = self; 
self.tableView.delegate = self; 
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 


self.timeSheetEntryTableView.allowsMultipleSelection = YES; 

self.array = [[NSMutableArray alloc]init]; 
[self.view addSubview:self.tableView]; 

} 

// Ячейка для строки с индексом пути

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

NSString *cellIdentifier = @"Cell Identifier"; 


self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 


if (!self.cell) { 

    self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:cellIdentifier]; 
    self.cell.accessoryType = UITableViewCellAccessoryNone; 

} 
self.cell.selectionStyle = UITableViewCellSelectionStyleNone; 
if ([self.array containsObject:indexPath]) 
{ 
    [self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]]; 
} 
else 
{ 
    [self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]]; 
} 

return self.cell; 

} 

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

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

self.cell = [tableView cellForRowAtIndexPath:indexPath]; 

[self.array addObject:indexPath]; 
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]]; 

} 

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

self.cell = [tableView cellForRowAtIndexPath:indexPath]; 

[self.array removeObject:indexPath]; 

[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]]; 

}