2010-11-03 2 views
0

Я хочу отображать пользовательские ячейки, но мой код отображает только одну пользовательскую ячейку таблицы сразу. Что я делаю неправильно?Пользовательский UITableViewCell только для рисования одной ячейки

У меня есть UIViewController nib, настроенный с моим UITableView внутри UIView. Также есть UITableViewCell в nib, чей класс - CustomCell (подкласс UITableViewCell). И UITableView, и Cell являются @sythesized IBOutlet @properties.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = @"CellIdentifier"; 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell 
    if (cell == nil) 
    { 
     cell = standardCell; // standardCell is declared in the header and linked with IB 
    } 
    return cell; 
} 

ответ

3

Вы можете использовать ниже пример кода;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = @"CellIdentifier"; 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell 
    if (cell == nil) 
    { 
    NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    for (id currentObject in objectList) { 
     if([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell*)currentObject; 
      break; 
     } 
    } 
    } 
    return cell; 
} 
2

Вы должны создать новую ячейку каждый раз, когда dequeueReusableCellWithIdentifier возвращение nil

Обычно это должно выглядеть

... 
if (cell == nil) 
{ 
    cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil] objectAtIndex:0] 
} 
... 

P.S. вместо objectAtIbndex: вы можете пройти через массив, возвращаемый и использовать isKingOfClass:[MyCell class] найти Клетку

2

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

if (cell == nil) { 
    /* instantiate cell or load nib */ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
} 

/* configure cell for a given index path parameter */ 
if (indexPath.row == 123) 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
else 
    cell.accessoryType = nil; 
/* etc. */ 

return cell; 
0

Если cell == nil, то вам необходимо создать экземпляр нового UITableViewCell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = @"CellIdentifier"; 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     // Instantiate a new cell here instead of using the "standard cell" 
     CustomCell *cell= [[[CustomCell alloc] init reuseIdentifier:CellIdentifier] autorelease]; 

     // Do other customization here 

    } 
    return cell; 
} 
Смежные вопросы