【问题标题】:IOS swift TableView is there a way I can reuse themIOS swift TableView有没有一种方法可以重用它们
【发布时间】:2017-03-29 22:05:55
【问题描述】:

我是 IOS 开发新手,正在使用 swift 3.0 。我有 2 个具有相同布局的自定义 TableView。唯一的区别是通过 Json URL 返回的数据。一个 TableView 称为 HomePageC ,另一个称为 UserProfileC 。如何使用我在 Prototype HomePageC 中的内容并在 UserProfileC 中重用它?这就像拥有一个数据主页然后看到单个用户数据因此 userProfile 但布局相同,因为我认为执行 2 个相同的 TableView 是多余的。

这是HomePageC代码

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! HomePageTVC


   cell.post.text = Posts[indexPath.row]
   cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)
   cell.fullname.tag = indexPath.row
   cell.fullname.addTarget(self, action: #selector(HomePageC.Fullname_Click(sender:)), for: .touchUpInside)
   cell.comments.setTitle(Comments[indexPath.row],for: UIControlState.normal)
   cell.comments.tag = indexPath.row
   cell.votes.setTitle(Votes[indexPath.row],for: UIControlState.normal)
   cell.votes.tag = indexPath.row
   cell.time.text = Times[indexPath.row]
  cell.shares.setTitle(Shares[indexPath.row],for: UIControlState.normal)
   cell.shares.tag = indexPath.row
  cell.location.text = Locations[indexPath.row]


        return cell
    }

UserProfileC 代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", for: indexPath) as! HomePageTVC


        cell.post.text = Posts[indexPath.row]
        cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)
        cell.fullname.tag = indexPath.row
        cell.fullname.addTarget(self, action: #selector(HomePageC.Fullname_Click(sender:)), for: .touchUpInside)
        cell.comments.setTitle(Comments[indexPath.row],for: UIControlState.normal)
        cell.comments.tag = indexPath.row
        cell.votes.setTitle(Votes[indexPath.row],for: UIControlState.normal)
        cell.votes.tag = indexPath.row
        cell.time.text = Times[indexPath.row]
        cell.shares.setTitle(Shares[indexPath.row],for: UIControlState.normal)
        cell.shares.tag = indexPath.row
        cell.location.text = Locations[indexPath.row]
        cell.vote_status.text = Votes[indexPath.row]


        return cell
    }

我试图将它投射到主页,但它给出了一个错误,任何帮助都会很棒,因为我很迷茫。

【问题讨论】:

    标签: ios swift uitableview swift3


    【解决方案1】:

    拥有可重用表格的一种可能方法是在 Storyboard 中创建一个单独的 UITableViewController 并在那里设计您的原型单元格。不再有两个不同的重用标识符——“HomePageTVC”和“UserProfileTVC”——你现在可以有一个(只留下“UserProfileTVC”)。不要忘记将表视图控制器的dataSourcedelegate 设置为自身。

    下一步:将容器视图添加到您想要在其中重用此表的任何视图控制器(在您的情况下包括 HomePageC 和 UserProfileC 控制器)。

    现在只需 Control+Drag 光标从创建的容器视图到 Storyboard 中可重用的表视图控制器,以建立 容器 关系。

    现在您有一个 UITableViewController 类来放置所有单元格管理逻辑,并且您可以根据当前的 parentViewController 属性轻松加载不同的数据 - 只需检查您的表当前嵌入的控制器类型:

    if parentViewController is HomePageC {
        // Load user data for Home page controller
    } else if parentViewController is UserProfileC {
        // Load user data for user profile controller
    }
    

    【讨论】:

    • 感谢您的完美声音
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多