在同一应用程序中为不同屏幕制作可重复使用的 tableview 的最佳方法是什么? [英] What is the best way to make a reusable tableview for different screens in the same application?

查看:21
本文介绍了在同一应用程序中为不同屏幕制作可重复使用的 tableview 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 swift 中开发一个类似于 Instagram 的社交 ios 应用程序.我有 2 个屏幕,其中包含几乎相同的提要显示.第一个是包含 tableview 的简单提要屏幕,第二个是包含个人资料信息的 tableview 标题的个人资料屏幕,tableview 应包含与第一个屏幕相同的数据.

I am working on a social ios application in swift similar to Instagram. I have 2 screens that contain almost the same display for feeds. The first one is a simple feeds screen that contains a tableview, the second one is the profile screen that contains a tableview header of profile info, and the tableview should contain same data of the first screen.

我能够做到这一点,但我不得不在第一个和第二个屏幕中为 tableview 重复相同的代码:(cellforRow、Number、数据和计算...)

I was able to do that but I had to repeat the same code for tableview in both first and second screen: (cellforRow, Number, data, and calculations...)

在这种情况下避免重复数据的最佳方法是什么?

what is the best approach to avoid duplicating data in such a case?

推荐答案

您可以通过编写一个单独的 tableview 委托和数据源处理程序类来实现这一点,该类可以代表视图控制器处理数据显示.

You can achieve this by writing an separate tableview delegate and data source handler class which can handle the data displaying on behalf for view controller.

处理程序:

import UIKit

class GenericDataSource: NSObject {

let identifier     = "CellId"
var array: [Any]           = []

func registerCells(forTableView tableView: UITableView) {
    tableView.register(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: identifier)
  }

func loadCell(atIndexPath indexPath: IndexPath, forTableView tableView: UITableView) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
    return cell
  }
}

// UITableViewDataSource
extension GenericDataSource: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return self.loadCell(atIndexPath: indexPath, forTableView: tableView)
    }

}
// UITableViewDelegate
extension GenericDataSource: UITableViewDelegate {

        func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)      {

        }
}
protocol GenericDataSourceDelegate: class {
            // Delegate callbacks methods
}

如何与视图控制器一起使用!

How to use it with view controller!

class MyViewControllerA: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var dataSource = GenericDataSource()


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self.dataSource
        self.tableView.dataSource = self.dataSource
    }
}

class MyViewControllerB: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var dataSource = GenericDataSource()


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self.dataSource
        self.tableView.dataSource = self.dataSource
    }
}

这篇关于在同一应用程序中为不同屏幕制作可重复使用的 tableview 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆