how to make a single class for table view and inherit in all other classes?

时间:2016-04-25 08:55:42

标签: ios objective-c inheritance tableview

I want to have table view in around 5 screens with same layout. what should be the best approach? Should i make a single class and then inherit other view controllers with that class? or make table view in each screens with separate coding?

2 个答案:

答案 0 :(得分:0)

You should create a subclass of UITableViewCell and use that as your cell layout through your app.

If you need the UITableView to have exactly the same properties throughout, you could also create a UITableView extension like this (in Swift)

extension UITableView {
    class func defaultTableView() -> UITableView {
        let tableView = UITableView()
        // Setup your default tableView properies here
        return tableView
    }
}

答案 1 :(得分:0)

这取决于你的5种形式之间的相似之处。

  1. 如果您有5种不同的表格,每种表格具有相同的表格视图(并且具有相同的数据),但表格上的其他控件可能不同,那么最好的方法是制作一个TTableView控制器。然后将ViewContainer放在5个表单中的每一个上,然后从容器中嵌入segue到单独的TableViewController。

  2. 如果您有5种不同的表格,但是这个表视图看起来很相似,那么最好的方法是使用自己的xib创建一个UITableViewCell,并在所有5个TableView中使用它(如tskippe的回答)。 / p>