开发者

change class of TableViewCell according to ViewController in which tableView is present

I have only 1 tableView class but 4 cell class. I'm adding the same tableView to all the viewControllers present in the app. The difference is the type (class,to be precise开发者_如何学C) of the cell. I want to change the class of cell according to the viewController in which the tableView is present. Also, the cell's content view is drawn in another subclass of the cell's class. Please provide a solution or approach to follow to make this functionality. Thanks, in advance.


You probably should be using a different table view for each view controller (if you are not already).

Typically, you would also use a different datasource/delegate for each table view (you could use each view controller, for example).

However, if you must use the same class as the datasource/delegate for all four of the table views, tableView:cellForRowAtIndexPath: (and the other delegate methods) give you the table view as the first argument, so you can return the correct cells like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == myTableViewOne) {
        // dequeue or allocate/init and configure/return the cell for the first table view here
    } else if (tableView == myTableViewTwo) {
        // dequeue or allocate/init and configure/return the cell for the second table view here
    } else if (tableView == myTableViewThree) {
        // dequeue or allocate/init and configure/return the cell for the third table view here   
    } else {
        // dequeue or allocate/init and configure/return the cell for the fourth table view here
    }
}


Ask the tableView for it's Class name using NSObjects -class method and use that to decide on what cell class to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜