How to use 2 UITableView in a UIViewController?

2020-03-24 09:05发布


Plz guide that how can i use 2 UItableView(or more) in a UIViewController & manage their
numberOfRowsInSection,......& other methods.
Any ideas for that??

5条回答
混吃等死
2楼-- · 2020-03-24 09:32

Assuming that your actual problem lies in assigning the same object as UITableViewDelegate:

UITableViewDelegete methods pass the UITableView instance to it. You just need to filter out which tableview you need to operate on. For example:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == yourFirstTableView) {
        // <do whatever on first table view>...
    } else if (tableView == yourSecondTableView) {
        // <do whatever on second table view>...
    }
}
查看更多
家丑人穷心不美
3楼-- · 2020-03-24 09:38

use tag.. for eg use table view1 as tag=0 and use table view2 as tag

查看更多
We Are One
4楼-- · 2020-03-24 09:46
// tableView parameter is the tableView for which the delegate method is called
// u can compare this with your table view references
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == myTableView1) {
        // return data for myTableView1
    } else if (tableView == myTableView2) {
        // return data for myTableView2
    }
}
查看更多
对你真心纯属浪费
5楼-- · 2020-03-24 09:51

why are you going to use two tableviews in UIviewcontroller?.It is possible,set tag to each of the tableview you are adding.u can check these tags in delegate methods and do changes.

查看更多
地球回转人心会变
6楼-- · 2020-03-24 09:56
if(tableView1){
    if(section==0)
        return 1; /*  1 row for section 0 in the first table.  */
    if(section==1)
        return 2; /*  2 rows for section 1 in the first table.  */
}

if(tableView2){
    if(section==0)
        return 3; /*  3 row for section 0 in the second table.  */
    if(section==1)
        return 2; /*  2 rows for section 1 in the second table.  */
}
查看更多
登录 后发表回答