Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple TableViews in a single screen
    primarykey
    data
    text
    <p>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.</p> <p>I am trying to understand if my approach is. [Edited 9/2 with correct code to make this approach work]</p> <p>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. </p> <p>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.</p> <p>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. </p> <p>Focusing on one of the TableViews, my ChatTableView. </p> <p>In my AppDelegate has a property for chatHistory of type ChatHistory which implements UITableViewDelegate &amp; UITableViewDataSource.</p> <p>// AppDelegate.h</p> <pre><code>ChatHistory *chatHistory; ... @property (nonatomic, retain) ChatHistory *chatHistory; </code></pre> <p>// ChatHistory.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface ChatHistory : NSObject &lt;UITableViewDelegate, UITableViewDataSource&gt; { UITableViewCell *nibLoadedCell; NSMutableArray *messages; } @property (nonatomic, retain) UITableViewCell *nibLoadedCell; @property (nonatomic, retain) NSMutableArray *messages; @end </code></pre> <p>// 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</p> <pre><code>#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 </code></pre> <p>// MyViewController.m</p> <pre><code>- (void)viewDidLoad { // MAKE SURE TO INITIALIZE viewDidLoad not InitWithNib if (!appDelegate.chatHistory) appDelegate.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]; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload