Search code examples
iosiphoneios7

How to solve these errors multiple views


i have multiple table view on a same view how to solve this error

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


    if (tableView == method) {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *methodcell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(methodcell == nil){
            methodcell = [[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
        }
        methodcell.textLabel.text = [methodarray objectAtIndex:indexPath.row];
        return methodcell;

    }

    else if (tableView == schedule){

        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *schedulecell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(schedulecell == nil){
            schedulecell = [[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
        }
        schedulecell.textLabel.text = [ScheduleArray objectAtIndex:indexPath.row];
        return schedulecell;
    }


}

Solution

  • You can deque cell outside if {} else if {}, configure it depending on tableView and return.

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewStylePlain
                                     reuseIdentifier:CellIdentifier];
    }
    if (tableView == method) {
       //configure cell for method tableView
    } else if (tableView == schedule) {
       //configure cell for schedule tableView
    }
    return cell;