2015-01-02 2 views
0

Прилагаемый код возвращает ошибку:управления может достигать конец непусто функции

Control may reach end non-void function 

Код:

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

    if (indexPath.row == 0) { 
     FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath]; 

     if (fCustomCell == nil) { 

      fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"]; 
     } 

     return fCustomCell; 

    } 

    else if (indexPath.row == 1) { 
     SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath]; 

     if (sCustomCell == nil) { 

      sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCustomCell"]; 
     } 

     return sCustomCell; 

    } 
} //<-- Control may reach non-void function (I precise that's the end of the cellForRowAtIndexPath method) 

Я знаю, что эта проблема особенно на «возвращение», но как Я устраняю ошибку?

ответ

4

Вы учитываете случаи, когда indexPath.row == 0 и где indexPath.row == 1, но компилятор говорит: «Что я должен вернуть, если строка не равна 0 или 1?

Возможно, вы захотите получить return nil; в конце вашего метода.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) 
    { 
     FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath]; 
     if (fCustomCell == nil) 
     { 
      fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"]; 
     } 
     return fCustomCell; 
    } 
    else if (indexPath.row == 1) 
    { 
     SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath]; 
     if (sCustomCell == nil) 
     { 
      sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondCustomCell"]; 
     } 
     return sCustomCell; 
    } 
    return nil; //<--Add this line 
} 

или, возможно, "еще" случай:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) 
    { 
     FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath]; 
     if (fCustomCell == nil) 
     { 
      fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"]; 
     } 
     return fCustomCell; 
    } 
    else if (indexPath.row == 1) 
    { 
     SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath]; 
     if (sCustomCell == nil) 
     { 
      sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondCustomCell"]; 
     } 
     return sCustomCell; 
    } 
    else //<--Add this clause 
    { 
     OtherCustomCell *oCustomCell = [tableView dequeueReusableCellWithIdentifier:@"otherCustomCell" forIndexPath:indexPath]; 
     if (oCustomCell == nil) 
     { 
      oCustomCell = [[OtherCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherCustomCell"]; 
     } 
     return sCustomCell; 
    } 
} 

Примечание: У вас также есть опечатка в ваших повторного использования идентификаторов:

"secondCustomCell" не то же самое, как "SecondCustomCell"

+0

Благодарим за ответ! Да для «secondCustomCell», который был из-за опечаток, когда я копирую/вставляю блок кода, я редактировал маленькие вещи перед ^^ – Lkm

1

Добавить линию return nil;.

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

    if (indexPath.row == 0) { 
     FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath]; 

     if (fCustomCell == nil) { 

      fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"]; 
     } 

     return fCustomCell; 

    } 

    else if (indexPath.row == 1) { 
     SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath]; 

     if (sCustomCell == nil) { 

      sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCustomCell"]; 
     } 

     return sCustomCell; 

    } 
    return nil; 
    } 

Я мог бы добавить, что вам нужно, чтобы убедиться, что вы fCustomCell и ваш sCustomCell не (!=) nil.

if (!fCustomCell) { 
    fCustomCell = [UITableViewCell alloc] initWithStyle:/*aStyle*/ reuseIdentifier:/*identifier*/]; 
} 

Идентификатор может быть статическим NSString определено в начале этого метода так: static NSString *identifier = @"cell";

Посмотрите на некоторые учебники.

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