什么时候可以继承UITableView? [英] When to subclass UITableView?

查看:49
本文介绍了什么时候可以继承UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MainViewController,我想向其中添加两个UITableView,每个UITableView具有不同的单元格.但是我不想通过检入表委托方法来阻塞我的MainViewController代码,然后对它进行操作.太乱了.

I have a MainViewController, and I want to add two UITableView's to it, each with different cells. But I don't want to clog up my MainViewController code by checking in the table delegate methods which table it is, and then acting on it. It gets too messy.

所以我想我会继承UITableView并让它自己处理cellForRow和其他表方法,这样,当我要将表添加到MainViewController时,我所要做的就是

So I thought I would subclass UITableView and let it handle the cellForRow and other table methods by itself, and this way when I want to add a table to MainViewController, all I'd have to is

CustomTable *customTable = [[CustomTable alloc] init];
[self.view addSubview:customTable];

,所有委托方法都将在该类中处理,而我的MainViewController会变得整洁.

and all the delegate methods would be handled in that class, leaving my MainViewController clutter free.

我要解决这个问题了吗?我应该代替UITableViewController的子类化吗?有什么区别?

Am I approaching this wrong? Should I be subclassing UITableViewController instead? What's the difference?

推荐答案

何时将 UITableView 子类化?不是现在.

When to subclasss UITableView? Not now.

创建两个类,它们是视图控制器的成员变量.将表视图委托指向您的两个新类中的每一个.

Create two classes, which are member variables of your view controller. Point the table view delegates at each of your two new classes.

在Cocoa中,您倾向于组合类而不是像在Java和C#中通常那样从类继承.

In Cocoa you tend to combine classes rather than inherit from them as you usually do in Java and C#.

这篇关于什么时候可以继承UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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