Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6536249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:28:12+00:00 2026-05-25T10:28:12+00:00

I have a UIViewController that I plan to have two TableViews and some other

  • 0

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)
                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];
        ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:28:12+00:00Added an answer on May 25, 2026 at 10:28 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIViewController that implements TableViews delegate and datasource protocols. Now I want
I'm trying to do some simple animation. I have a UIViewController that has two
I have a UIViewController that manages the display of some data. When the user
I have a UIViewController that I want to add on top of the entire
I have a UIViewController that allow me to display some text into a view.
I have a UIViewController that I want to display a second UIViewController as a
I have a UIViewController that I want to load from a NIB that has
I have a UIViewController that has both a UITable and a UIView on it.
I have a root UIViewController subclass that has a UITabBar, (I'm not using UITabBarController)
I have a UIViewController that has a XIB. I don't want to get rid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.