Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Multiple TableViews in a ViewController

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 394
    Comment on it

    Adding a UITableView in a ViewController is a great way to display data and information in a list, and it is easy to implement as well.

    The problem arises when the developer have to implement more than one table views in a viewController using a single set of delegate methods. As it turns out it is quite simple to display two or more than two UITableViews in a ViewController.

    The trick is to make IBOutlet of each and every table view that has to be displayed.

    The next step includes using an (if-else) ladder to choose between the table views.

    The Switch-Case statement will work equally well in this case.

    Alternatively the developer can give unique tags to the tables and access them through their tags, then use the (if-else) ladder, or Switch-Case statement to choose between them.

    The required code to implement this method is as follows.

    #pragma mark Table View Delegates
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        if (tableView == table1)
        {
            return 1;
        }
        else if (tableView == table2)
        {
            return 1;
        }
        else if (tableView == table3)
        {
            return 1;
        }
    return 0;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == table1)
        {
            return [array1 count];
        }
        else if (tableView == table2)
        {
            return [array2 count];
        }
        else if (tableView == table3)
        {
            return [array3 count];
        }
        return 0;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell = nil;
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        if (tableView == table1)
        {
            cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
            return cell;
        }
        else if (tableView == table2)
        {
        cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
        return cell;
        }
        else if (tableView == table3)
        {
        cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
        return cell;
        }
        return 0;
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == table1)
        {
    
        // Put your code here.
    
        }
        else if (tableView == table2)
        {
    
        // Put your code here.
    
        }
        else if (tableView == table3)
        {
    
        // Put your code here.
    
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: