我正在尝试在 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 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;
}
关于ios - UITableView 中的两个自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459087/