如何在iOS中的单个视图控制器中使用多个uitableview -swift



我是iOS开发的新手。当前,我正在研究一个项目,在一个项目中,我在一个单个视图控制器中使用了两个以上的UITableView,但是两个数据源都来自服务器。当第一个API命中时,它显示结果,但是从该列表中选择项目后,我无法在列表中显示响应。

这是我的代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("sdfsdfsf")
    var count:Int?
    if tableView == self.pat_search_listview {
        count = serach_data.count
    }
    else  if tableView == self.visit_listview {
        count = all_vist_data.count
    }
    return count!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    if tableView == self.pat_search_listview {
        cell.textLabel?.text = serach_data[indexPath.row].name + "     " + serach_data[indexPath.row].id
    }
        
        
    else  if tableView == self.visit_listview {
        print("second listview")
        cell.textLabel?.text = all_vist_data[indexPath.row].date
    }
    
    return cell
}

检查两个桌面的插座连接...都不应该在viewDidload中nil

nil

在ViewDidload中:

self.pat_search_listview.dataSource = self; 
self.visit_listview = self;
self.pat_search_listview.tag = 0
self.visit_listview.tag = 1

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 0
...
else
...

确保设置委托和数据源,并且在添加/更新数组后不要忘记重新加载表。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if tableView == self.pat_search_listview
    {
        return serach_data.count/*pat_search_listview's Array count*/
    }
    else  if tableView == self.visit_listview
    {
        return all_vist_data.count/*visit_listview Array count*/
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    if tableView == self.pat_search_listview
    {
        cell.textLabel?.text = serach_data[indexPath.row].name + "     " + serach_data[indexPath.row].id
    }
    else  if tableView == self.visit_listview
    {
        print("second listview")
        cell.textLabel?.text = all_vist_data[indexPath.row].date
    }
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if tableView == self.pat_search_listview
    {
        //--- Item at index from pat_search_listview
    }
    else  if tableView == self.visit_listview
    {
        print("second listview")
        //--- Item at index from visit_listview
    }
}

最新更新