две пользовательские ячейки в UITableView

#ios #uitableview

#iOS #uitableview uitableview

Вопрос:

Я пытаюсь использовать две пользовательские ячейки внутри UITableView. Проблема, с которой я сталкиваюсь, заключается в том, что мое возвращение вызывается не в том месте. Я не уверен, что мне нужно для реализации return, чтобы остановить элемент управления от достижения конца не-void функции.

Вот код внутри моего cellforrowatindex.

 static NSString *kCellID = @"MessageCell";
static NSString *UCellID = @"UnreadCell";
if (status == 0)
{
    //static NSString *kCellID = @"MessageCell";
    MessageListCell *cell = (MessageListCell *) [tableView dequeueReusableCellWithIdentifier:kCellID];

        if (priority == 0)
        {
            cell. imageviewImportance.image = nil;
        }
        if (priority == 1)
        {
            cell.imageviewImportance.image = [UIImage imageNamed:@"single exclamation.png"];
        }
        if (priority == 2)
        {
            cell.imageviewImportance.image = [UIImage imageNamed:@"double exclamation mark.png"];
        }
        if (priority == 3)
        {
            cell.imageviewImportance.image = [UIImage imageNamed:@"triple exclamation kmar.png"];
        }
//if (status == 0)
//{
    cell.backgroundColor = [UIColor clearColor];
    cell.labelFrom.text = [NSString stringWithFormat:@"From: %@",m.From];
    cell.labelFrom.font = [UIFont boldSystemFontOfSize:17];
    cell.labelSubject.text = subject;
    cell.labelSubject.font = [UIFont boldSystemFontOfSize:17];
    cell.labelTime.text = time;
    cell.labelTo.text = [NSString stringWithFormat:@"To: %@",m.To];
    cell.labelTo.font = [UIFont boldSystemFontOfSize:17];
    return cell;
}
else if (status != 0)
{
    //static NSString *UCellID = @"UnreadCell";
    MessageListCell *cell = (MessageListCell *)[tableView dequeueReusableCellWithIdentifier:UCellID];
    cell.backgroundColor = [UIColor clearColor];
    cell.unreadLabelFrom.text = [NSString stringWithFormat:@"From: %@",m.From];
    cell.unreadLabelSubject.text = subject;
    cell.unreadLabelTo.text = [NSString stringWithFormat:@"To:%@",m.To];
    cell.unreadLabelTime.text = time;
    return cell;
}
//NSLog(@"description = %@",[cell description]);
//return cell;
 

}

Ответ №1:

Просто измените значение else if на else , тогда компилятор перестанет жаловаться.

То, как вы это делаете сейчас, говорит компилятору проверить 2 условия, что, если ни одно из них не выполнено? что будет возвращено?

Вам понадобится else блок, но поскольку else if он избыточен, это то else , что вам нужно.


 if (status == 0)
{
    static NSString *kCellID = @"MessageCell";
    MessageListCell *cell = (MessageListCell *) [tableView dequeueReusableCellWithIdentifier:kCellID];

    NSString *imageName;
    switch (priority) {
        case 0:
            imageName = nil;
            break;
        case 1:
            imageName = @"single exclamation.png";
            break;
        case 2:
            imageName = @"double exclamation mark.png";
            break;
        case 3:
            imageName = @"triple exclamation kmar.png";
            break;
        default:
            break;
    }


    cell.imageviewImportance.image = [UIImage imageNamed:imageName];
    cell.backgroundColor = [UIColor clearColor];
    cell.labelFrom.text = [NSString stringWithFormat:@"From: %@",m.From];
    cell.labelFrom.font = [UIFont boldSystemFontOfSize:17];
    cell.labelSubject.text = subject;
    cell.labelSubject.font = [UIFont boldSystemFontOfSize:17];
    cell.labelTime.text = time;
    cell.labelTo.text = [NSString stringWithFormat:@"To: %@",m.To];
    cell.labelTo.font = [UIFont boldSystemFontOfSize:17];
    return cell;
}
else
{
    static NSString *UCellID = @"UnreadCell";
    MessageListCell *cell = (MessageListCell *)[tableView dequeueReusableCellWithIdentifier:UCellID];
    cell.backgroundColor = [UIColor clearColor];
    cell.unreadLabelFrom.text = [NSString stringWithFormat:@"From: %@",m.From];
    cell.unreadLabelSubject.text = subject;
    cell.unreadLabelTo.text = [NSString stringWithFormat:@"To:%@",m.To];
    cell.unreadLabelTime.text = time;
    return cell;
}
 

Комментарии:

1. Спасибо. я бы поддержал ответ, но у меня недостаточно очков репутации. Спасибо вам за помощь.

2. @user3728381 Добро пожаловать, не беспокойтесь об этом, я проголосовал за вас 😉