Dancer
Dancer

Reputation: 17701

Xcode / ObjectiveC - Convert UITableViewController into TableView embedded in a UIViewController

I am fairly new to this Native App dev - I have built an app which contains a UITableViewController to display messages - all works fine - but for styling reasons I need to change it from a TableViewController to a tableview embedded within a viewcontroller.

I have made a view controller containing a table view and relevant linked custom cells / fields and altered the associated header file to -

 @interface NotificationsListTVController : UIViewController

but my table methods no longer fire and I'm not sure how to instantiate them?

(code below) #pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return self.GPTNotifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifierRead = @"CellRead";

UITableViewCell *cell;


notifications *n = [self.GPTNotifications objectAtIndex:indexPath.row];



   if (n.read == false) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];



    CustomCellRead *cellReadB = (CustomCellRead *)cell;
    cellReadB.notifTitle.text = n.notifTitleD;
    cellReadB.notifDate.text = n.notifDateD;
    cellReadB.notifMsg.text = n.notifMessage;


 return cellReadB;
}

 else {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRead     forIndexPath:indexPath];



    CustomCell *cellReadB = (CustomCell *)cell;
    cellReadB.notifTitle.text = n.notifTitleD;
    cellReadB.notifDate.text = n.notifDateD;
    cellReadB.notifMsg.text = n.notifMessage;


    return cellReadB;

    }


 }

Upvotes: 0

Views: 1573

Answers (3)

Benny Dalby
Benny Dalby

Reputation: 182

Make the following changes in NotificationsListTVController.h:

@interface NotificationsListTVController : UIViewController<UITableViewDataSource,UITableViewDelegate>

Also in NotificationsListTVController.m, dont forget to provide these two statements as well.

 tableView.delegate=self ;
 tableView.dataSource=self;

These are required to set the delegate methods.These two statements need to be provided after initializing your tableView. Like for instance :

   tblView = [[UITableView alloc] initWithFrame:CGRectMake(100,200,320,420) style: UITableViewStyleGrouped];


   tblView.delegate = self;
   tblView.dataSource = self;

   [self.view addSubview:tblView];

These methods you are referring to are the delegate methods which cannot be fired directly unlike other ordinary methods.

Hope it Helps!!!

Upvotes: 0

BGC
BGC

Reputation: 25

I do it this way in Interface Builder:

  • Make your TableViewController
  • Make your ViewController and add a ContainerView to it
  • Delete the segued embedded ViewController that comes with it
  • Select the ContainerView and draw a connection from viewDidLoad to your TableViewController
  • you'll get only once option: embed

Done. Your TableViewController will now get displayed within your ViewController.

Pass whatever Data you need forward from the ViewController to the TableViewController with the embedded Segue.

Upvotes: 1

Antonio MG
Antonio MG

Reputation: 20410

Are you setting the delegate and datasource of your tableview to your class?

Something like:

self.myTableView.delegate = self;
self.myTableView.dataSource = self;

When you create a UITableViewController this is done for you, but if you add the table yourself you need to set them.

Also:

@interface NotificationsListTVController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Upvotes: 2

Related Questions