Reputation: 23
I am learning switch function, so I changed the if and else statements like this:
but it gives me "Expected expression" error.
Why?
switch (indexPath.section) {
case 0: return {
cell.textLabel.text = FileANames[indexPath.row];
cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
}
case 1: return {
cell.textLabel.text = FileBNames[indexPath.row];
cell.detailTextLabel.text = FileBDetails[FileBNames[indexPath.row]];
}
case 2: return {
cell.textLabel.text = FileCNames[indexPath.row];
cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];
}
default: return cell;
}
//this one is correct
if (indexPath.section == 0) {
cell.textLabel.text = FileANames[indexPath.row];
cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
}
else if (indexPath.section == 1) {
cell.textLabel.text = FileBNames[indexPath.row];
cell.detailTextLabel.text = FileBDetails[FileBNames[indexPath.row]];
}
else if (indexPath.section == 2) {
cell.textLabel.text = FileCNames[indexPath.row];
cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];
}
return cell;
}
Thank you.
Upvotes: 0
Views: 2067
Reputation: 8491
It should be this
switch (indexPath.section) {
case 0: {
cell.textLabel.text = FileANames[indexPath.row];
cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
break;
}
case 1: {
cell.textLabel.text = FileBNames[indexPath.row];
cell.detailTextLabel.text = FileBDetails[FileBNames[indexPath.row]];
break;
}
case 2: {
cell.textLabel.text = FileCNames[indexPath.row];
cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];
break;
}
default: break;
}
return cell;
Upvotes: 1