开发者

Multiple TableViews in a single screen

I have a UIViewController that I plan to have two TableViews and some other items in. Both TableViews I am using on other screens, so I want to make them as independent and reusable as possible. One of those TableViews is called messageList (A UITableView) which shows my ChatHistory.

I am trying to understand if my approach is. [Edited 9/2 with correct code to make this approach work]

One approach would be to use a single table with 2 different sections, then in the delegate methods use a conditional statement to see which section is which and act accordingly.

The problem with this approach is usability. I want to easily reuse my TableViews in other views where one or the other TableView may or may not exist. Additionally, I want the DataSource to exist throughout the lifecycle of the app regardless of what Controller is instantiated or active.

My approach is to separate the view controller that manages the table view's from the table UITableViewDataSource and UITableViewDelegate implementations. But I am having a problem making this work.

Focusing on one of the TableViews, my ChatTableView.

In my AppDelegate has a property for chatHistory of type ChatHistory which implements UITableViewDelegate & UITableViewDataSource.

// AppDelegate.h

ChatHistory *chatHistory;
...
@property (nonatomic, retain) ChatHistory *chatHistory;

// ChatHistory.h

#import <Foundation/Foundation.h>

@interface ChatHistory : NSObject <UITableViewDelegate, UITableViewDataSource> {
    UITableViewCell *nibLoadedCell;
    NSMutableArray *messages;
}
@property (nonatomic, retain) UITableViewCell   *nibLoadedCell;
@property (nonatomic, retain) NSMutableArray    *messages;

@end

// ChatHistory.m - Note this code, including the custom cell was working correctly when it was a part of the controller so I believe it should be correct

#import "ChatHistory.h"
#include "ChatMessage.h"

@implementation ChatHistory
@synthesize nibLoadedCell;   // custom cell design
@synthesize messages;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
return [messages count];
}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:@"Discussion"];

}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ChatTableCell" owner:self options:nil];
    cell = nibLoadedCell;
    }
    // custom tag order - username; message; future - Avatar; like; dislike
ChatMessage *obj = [messages objectAtIndex:indexPath.row];
UILabel *messageLabel = (UILabel *) [cell viewWithTag:1];
messageLabel.text = obj.message;
UILabel *usernameLabel = (UILabel *)[cell viewWithTag:2];
usernameLabel.text = obj.sender;
return cell;
}

- (void)dealloc {
    if (messages) [messages release];
    [super dealloc];
}
@end

// MyViewController.m

- (void)viewDidLoad {   // MAKE SURE TO INITIALIZE viewDidLoad not InitWithNib
        if (!appDelegate.chatHistory)
                a开发者_JS百科ppDelegate.chatHistory = [[ChatHistory alloc] init];

        messageList = [[UITableView alloc] initWithFrame:CGRectMake(0, 54, 320, 100) style:UITableViewStylePlain];
        messageList.dataSource = appDelegate.chatHistory;
        messageList.delegate = appDelegate.chatHistory;
        [self.view addSubview:messageList];
        ...


You do not need to make a view controller your tableview DataSource or delegate; any object can be set. You can use a singleton as Felix suggests, or any other class structure you want. Since you mention that you want the chat history to be available from anywhere in the app, it makes sense to provide a UITableViewDataSource protocol to that chat history.

As for the UITableViewDelegate, you can simply create a new class as a subclass of NSObject and implement the delegate there. Make sure it's created and retained properly, and set in (upon load) as the delegate for your table views.


If you want one central data store, you could create a Singleton class with the data. Then set it as the data source for the table view or fetch the array (or whatever you got) from the data store in your UIViewController / UITableViewController.

If you initialize the data store in your AppDelegate, you can access it from every class you want (note that all data you load, will remain in memory until your application gets terminated by iOS)

How to create a Singleton class in Objective-C


I would do it this way: make the two UITableViews subviews of one view controller.

In each of the datasource methods you simply distinguish between the two tables, similar to the code Apple provides in the UISearchDisplayController examples. For instance:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (tableView == myTableView1) {
    // return appropriate number of rows
  }
  // return appropriate number of rows for the other table view
}

I know this does not separate the two table view classes very neatly. You would have to have a view controller controlling two other view controllers, one for each tableview.

Perhaps one has to consider the tradeoff between reusability and the complications of a more convoluted architecture (which typically also leads to classes that do not perform all that well in terms of reusability). That's why I would recommend the approach from the Apple sample projects.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜