How do I return two UITableViewCells?

how to return two custom uitableviewcells

  • Hi just trying to figure out how to load two different custom uitableviewcells into two different sections on my uitableview... Just not sure on how to proceed... here is the code I have currently - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Registration Button static NSString *CellIdentifier = @"CustomRegCell"; static NSString *CellNib = @"LogInCustomCell"; UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil]; cell = (UITableViewCell *)[nib objectAtIndex:0]; } return cell; } /////// NEW ATTEMPT.... :( if you could call it that.. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { //Registration Button static NSString *CellIdentifier = @"CustomRegCell"; static NSString *CellNib = @"LogInCustomCell"; UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil]; cell = (UITableViewCell *)[nib objectAtIndex:0]; } return cell; } else if (indexPath.section == 1) { //Registration Button static NSString *CellButtonIdentifier = @"CustomButtonCell"; static NSString *CellButtonNib = @"LogInCustomCell"; UITableViewCell *cellButton = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellButtonIdentifier]; if (cellButton == nil) { NSArray *nibButton = [[NSBundle mainBundle] loadNibNamed:CellButtonNib owner:self options:nil]; cellButton = (UITableViewCell *)[nibButton objectAtIndex:0]; } return cellButton; } return nil; }

  • Answer:

    Use the section property of the indexPath variable: if (indexPath.section == 0) { // do this } else if (indexPath.section == 1) { // do that } The section number is based on the order in which they appear. The first section you want to show in the tableView will be 0, and so on.

tinhead at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.