2012-06-27 3 views
0

Очень любопытно, как обрабатывать пользовательские ячейки таблицы изнутри tableView: cellForRowAtIndexPath :.Создание пользовательских ячеек таблицы UITableView изнутри cellForRowAtIndexPath

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

Благодаря

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Load custom table cell 
    UINib *customCellNib = [UINib nibWithNibName:@"CustomItem" bundle:nil]; 

    //Register this nib, which contains the cell 
    [[self tableView] registerNib:customCellNib forCellReuseIdentifier:@"CustomItemCell"]; 


    //.... More stuff here 

    } 


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

    CustomItemCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell"]; 

    // If there is no reusable cell of this type, create a new one 
    if (!cell) { 


     //Is this right? 
     cell = [[CustomItemCell alloc] init]; 

    } 




      //Set up cell with data here... 



    return cell; 
} 

ответ

0

Я в конечном итоге решить это путем доступа к основному пакету, ищет файл ячейка бобах пользовательские таблиц. Когда мы находим наш собственный класс ячеек таблицы, мы назначаем iVar «ячейку» этому объекту.

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

    static NSString *cellID = @"CustomItemCell"; 
    CustomItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

    // If there is no reusable cell of this type, create a new one 

    if(cell == nil){ 


    NSArray *nibObjects = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil]; 
    for (id currentObject in nibObjects) { 
     if([currentObject isKindOfClass:[CustomItemCell class]]) 
     { 
      cell = (CustomItemCell *)currentObject; 

     } 


    } 

    } 
    //Assign data to cell here 
} 
Смежные вопросы