Lovely Lovely - 1 month ago 7
iOS Question

multiple custom cells-error in code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellIdentifier = @"cellWithTextField";
static NSString *cellIdentifier1 = @"cellWithLabel";
static NSString *cellIdentifier2 = @"cell";

if (indexPath.section == 0) {
UserDetailCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithTextField"];
cell.userDetailLabel.text = [userDetail objectAtIndex:indexPath.row];
return cell;
}
else if (indexPath.section == 1) {
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithLabel"];
cell.userInfoLabel.text = [userInfo objectAtIndex:indexPath.row];
return cell;
}
else if (indexPath.section == 2)
{
NewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.detailLabel.text = [detail objectAtIndex:indexPath.row];
return cell;
}
}


This code shows error......control may reach non void function...what should I return...where UI am going wrong?

Answer

You need to define one more condition on else like this

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier  = @"cellWithTextField";
    static NSString *cellIdentifier1 = @"cellWithLabel";
    static NSString *cellIdentifier2 = @"cell";

    if (indexPath.section == 0) {
        UserDetailCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithTextField"];
        cell.userDetailLabel.text  = [userDetail objectAtIndex:indexPath.row];
        return cell;
    }
    else if (indexPath.section == 1) {
        DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithLabel"];
        cell.userInfoLabel.text  = [userInfo objectAtIndex:indexPath.row];
        return cell;
    }
    else if (indexPath.section == 2)
    {
        NewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.detailLabel.text  = [detail objectAtIndex:indexPath.row];
        return cell;
    } else {
        // Some code to return default cell or return nil 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        return cell;
    }

}