【问题标题】:How to use multiple UITableView in single view Controller in iOS - Swift如何在 iOS 的单视图控制器中使用多个 UITableView - Swift
【发布时间】:2021-08-05 10:21:14
【问题描述】:

我是 iOS 开发的新手。目前,我正在做一个项目,其中我在单个视图控制器中使用了两个以上的UITableViews,但是两个数据源一个一个地来自服务器。当第一个 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
}

【问题讨论】:

  • 它是正确的。这段代码面临什么问题?
  • 你在屏幕上看到的是哪一个tableView?
  • 问题是它在第二个 uitableview 中什么都不显示
  • ite 不显示任何内容
  • @PardeepKumar 这条线 print("second listview") 被执行了吗?

标签: ios swift xcode uitableview


【解决方案1】:

检查两个 tableViews 的 Outlet 连接...在 viewDidLoad 中两者都不应该为零

在 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
...

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 感谢大家给我的支持或想法
    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2017-10-02
    相关资源
    最近更新 更多