2017-04-18 125 views
-1

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

这里是我的代码:提前

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 
} 

感谢

+0

它是正确的。这个代码面临什么问题? – KKRocks

+0

您在屏幕上看到的tableView中的哪一个? –

+0

的问题是,它显示没有在第二个uitableview –

回答

0

同时检查tableViews的出口连接......都应该不是零在viewDidLoad中

在viewDidLoad中

self.pat_search_listview.dataSource = self; 
self.visit_listview = self; 

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

in

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

if tableView.tag == 0 
... 
else 
... 
-1

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

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 
    } 
} 
+0

感谢大家,给我你的支持或想法 –

+0

欢迎..... :) –

相关问题