문제

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.

도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top