ios - UITableView 中的两个自定义单元格

标签 ios 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;

}

最佳答案

只需更改 else ifelse ,那么编译器将停止提示。

你现在拥有它的方式是告诉编译器检查 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;
}

关于ios - UITableView 中的两个自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459087/

相关文章:

objective-c - 将核心数据值放入数组中

ios - 如何检查 UITableview 中 UIButtons 的条件

ios - 自调整单元格对我不起作用

objective-c - 在我滚动之前,数据不会加载到 UITableView 中

ios - 无法在文件夹中写入 nsdata

ios - 具有默认 nil 值的 UIDatePicker - iOS

ios - UISearchbar 以编程方式添加时不调用 didSelectRowAtIndexPath

"received memory warning"消息后 iOS UITableView 内容松散

ios - 为什么我的 tableView 函数运行了 3 次?

ios - iBeacon:根据您当前的位置唤醒应用程序