2013-04-29 7 views
0

Я использую Coredata и NSFetchedResultsController для хранения и извлечения значений и отображения их в виде таблицы. Я создаю пользовательскую метку в cellForRowAtIndexPath и отображаю значение атрибута 'lastname'. Но я ошибаюсь.UILabel показывает неправильные значения в UITableView

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
UILabel *label = nil; 
if(cell == nil){ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease]; 
    label = [[[UILabel alloc]initWithFrame:CGRectMake(160,10,120,21)]autorelease]; 
    label.backgroundColor = [UIColor clearColor]; 
    [cell.contentView addSubview:label]; 

    //Configure the cell 
    List *list = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    label.text = list.lastname; 

} 
[self configureCell:cell atIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
cell.selectionStyle = UITableViewCellSelectionStyleGray; 
return cell; 
} 

странно то, что она работает нормально, если я удалить UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; линию и если условие.

ответ

0

Вы должны переместить

label.text = list.lastname; 

вне

if(cell == nil) 

Причина заключается в том, что содержание внутри if(cell == nil) будет вызван только х раз, где х число видимых ячейки на экране. Когда вы прокручиваете, новые ячейки повторно используются, и поэтому они содержат некорректное значение.

EDIT:

Вам также необходимо переместить

List *list = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

за пределами if.

EDIT 2:

Вот как это должно выглядеть следующим образом:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
UILabel *label = nil; 
if(cell == nil){ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease]; 
    label = [[[UILabel alloc]initWithFrame:CGRectMake(160,10,120,21)]autorelease]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.tag=1; 
    [cell.contentView addSubview:label]; 


} 
[self configureCell:cell atIndexPath:indexPath]; 

//Configure the cell 
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
label = (UILabel*)[cell.contentView viewWithTag:1]; 
label.text = list.lastname; 

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
cell.selectionStyle = UITableViewCellSelectionStyleGray; 
return cell; 
} 

это должно работать для вас

+0

до сих пор не работает –

+0

Отредактированный мой ответ. Это должно теперь работать – gasparuff

+0

поместить его после вашей '[self configureCell: cell atIndexPath: indexPath];' – gasparuff

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