Multiple UITableview in Single Viewcontroller

后端 未结 5 1904
無奈伤痛
無奈伤痛 2020-12-09 10:16

I have a viewcontroller in that i want to show 3 tableviews(because the content and the table properties are different). How do i add these delegat

5条回答
  •  清歌不尽
    2020-12-09 11:07

    You always get a reference and can always check for which tableView delegate or dataSource method is called.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        if (tableView == self.tableView1)
        {
            return 1;
        }
    
        if (tableView == self.tableView2)
        {
            return 1;
        }
    
        if (tableView == self.tableView3)
        {
            return 1;
        }
    }
    

    You don't gain anything by using same identifier for all tables. Use something like:

    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {    
        if (tableView == self.tableView1)
        {
            static NSString *CellIdentifier1 = @"cellForTable1";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    
            if (!cell)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
            }
    
            cell.textLabel.text = [NSString stringWithFormat: @"table1: %d.%d", indexPath.section, indexPath.row];
    
            return cell;
        }
    
        if (tableView == self.tableView2)
        {
            static NSString *CellIdentifier2 = @"cellForTable2";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    
            if (!cell)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
            }
    
            cell.textLabel.text = [NSString stringWithFormat: @"table2: %d.%d", indexPath.section, indexPath.row];
    
            return cell;
        }
    
       if (tableView == self.tableView1)
        {
            static NSString *CellIdentifier3 = @"cellForTable3";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
    
            if (!cell)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
            }
    
            cell.textLabel.text = [NSString stringWithFormat: @"table3: %d.%d", indexPath.section, indexPath.row];
    
            return cell;
        }   
    }
    

提交回复
热议问题