Strange error: insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException - objective-c

I have searched for a more fitting answer to NSInternalInconsistencyException I receive in the following sample app I wrote, but still nothing. The goal is to create an expand/collapse functionality for the top row in each section of the tableView. Right now I try to implement the expand part, and this works for row 0 in section 0. As soon as the user taps row 0 in another section this error appears:
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to resolve row for index path: 2 indexes [0, 1]'
This is strange since I store each and every UITableViewCell for the table in a mutable array of arrays. NSMutableArray *cellForRow, where each index represents a section in the table and each object is an object of type NSMutableArray. I do this to avoid any issues arising from queueing reusable cells that I first thought triggered the above exception.
The exception happens at the insertRowsAtIndexPaths statement. I read earlier here that the UITableViewController code must keep track of changes to the number of rows caused by insertions/deletion. I believe I do that with NSMutableArray *rowsInSection so that the UITableView data source method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
returns the correct number of rows in a section after a change.
What am I doing wrong in my code to get the above mentioned exception?
This is the interface file:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface MasterViewController : UITableViewController {
NSMutableArray *rowsInSection;
NSMutableArray *cellForRow;
}
#property (nonatomic,strong) NSMutableArray *rowsInSection;
#property (nonatomic,strong) NSMutableArray *cellForRow;
#end
And this is the implementation file:
#import "MasterViewController.h"
const NSInteger numSections = 4;
const NSInteger numRows = 1 + 4;
const NSInteger addRemoveRows = 4;
#implementation MasterViewController
#synthesize rowsInSection;
#synthesize cellForRow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Table View";
rowsInSection = [NSMutableArray arrayWithCapacity:numSections];
cellForRow = [NSMutableArray arrayWithCapacity:numSections];
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.dataSource = self;
self.tableView.delegate = self;
// add number of rows for section
for (NSInteger i = 0; i < numSections; i++) {
[self.rowsInSection addObject:[NSNumber numberWithInteger:1]];
}
// container for reusable table view cells
for (NSInteger i = 0; i < numSections; i++) {
NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:numRows];
for (NSInteger j = 0; j < numRows; j++) {
// top row in section
if (j == 0) {
UITableViewCell *topCell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
topCell.accessoryType = UITableViewCellAccessoryNone;
topCell.contentView.backgroundColor = [UIColor whiteColor];
topCell.textLabel.textColor = [UIColor blueColor];
[rowsArray addObject:topCell];
// the rest
} else {
UITableViewCell *simpleCell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
simpleCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
simpleCell.textLabel.textColor = [UIColor whiteColor];
[rowsArray addObject:simpleCell];
}
}
// add rows for current section into cell container
[self.cellForRow addObject:rowsArray];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return numSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = [(NSNumber *)[self.rowsInSection objectAtIndex:section] integerValue];
//NSLog(#"%#",self.rowsInSection);
//NSLog(#"Rows: %d in section: %d",rows,section);
return rows;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell.
// row count
NSLog(#"Rows: %d in section: %d",[tableView numberOfRowsInSection:indexPath.section],indexPath.section);
if (indexPath.row == 0) {
UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = #"TOP ROW";
NSLog(#"Row: %d in section: %d - %#",indexPath.row,indexPath.section,cell);
return cell;
} else {
UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"row: %d",indexPath.row];
NSLog(#"Row: %d in section: %d - %#",indexPath.row,indexPath.section,cell);
return cell;
}
// not reaching here
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:#"Section %d",section];
}
#pragma mark - Row editing
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// add table view cells to section if tapped on top row
if (indexPath.row == 0 && [tableView numberOfRowsInSection:indexPath.section] == 1) {
//NSLog(#"Selected row: %d in section: %d",indexPath.row,indexPath.section);
NSMutableArray *indexPathArray = [NSMutableArray array];
for (NSInteger i = 1; i <= addRemoveRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[indexPathArray addObject:indexPath];
}
// update row count for section
NSInteger newRowCount = addRemoveRows + 1; // +1 for existing top row
[self.rowsInSection replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInteger:newRowCount]];
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
}
}
#end

If you are inserting/deleting multiple rows at the same time it has to be bracketed with calls to beginUpdates/endUpdates.
The table view data source needs to be consistent with your insert/delete calls.
Set a break point in numberOfRowsInSection to assure that the number of rows in a section is correct after the insert/delete rows.
(i'm not going to read through all your code to debug it for you.)
good luck!

I was having this issue as well.
It wasn't something I was doing wrong inside the body of code, but I didn't realize an object was being inserted into my dataSource at another time. If you're getting this issue, make sure you follow bshirley's advice and stick a breakpoint on numberOfRowsInSection AND the body of code where you add the item. Make sure the number is the same amount of items in your dataSource throughout the lifecycle of adding the data. There really isn't any reason it should fail.
It is fairly simple, just make sure you're keeping your data and indexpath amounts before and after the update. Otherwise this exception will be thrown.

You are not only inserting cells, you also deleting old row, but the table view does not know about it as you haven't told it. So the table view "knows" it has one row, then you tell it you've added lets say two rows. The table view knows, that it should contain 3 rows BUT it founds only two as you've deleted the old one... What you need is or to use -(void)tableView:(UITableView)tableView deleteRowsAtIndexPaths:(NSArray)indexPath withRowAnimation:(UITableViewRowAnimation)animation; or delete one index path from array of added rows...And by the way, return to use reusable cells as it have no connection with the error you've faced...

In this method
-(void)tableView:(UITableView)tableView deleteRowsAtIndexPaths:(NSArray)indexPath withRowAnimation:(UITableViewRowAnimation)animation;
your variable NSIndexPath *indexPath is repetition with the variable of this method

Related

Counting elements in array and creating UILabels and UIImage iOS6

I`m trying to implement the style of the Label-Image-Label etc.. in UItableviewCell, each row might have 3 or 4 or 5 labels with image between them. The text for the labels are stored in plist as array/dictionary ,keys are airprort1 airport2 etc.
I`m stuck at setting the cells and labels and image programmatically.
Tried with storyboard yet couldn`t figure the logic.
Here is my code so far:
#import "ShowLabelTVC.h"
#interface ShowLabelTVC ()
#end
#implementation ShowLabelTVC
#synthesize flights;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"flight" ofType:#"plist"];
flights = [[NSMutableArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#", flights);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return flights.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TagCell";
TagCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TagCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
//for (int i = 0; i < flights.count; i++)
//{
// cell.sec1Label.text = [[flights objectAtIndex:indexPath.row] valueForKey:#"airport1"];
// if(i != flights.count - 1)
// {
// }
//}
return cell;
}
#end
Cameron what i want to acomplish is to have each cell show the correct amount of labels seperated by an image.
For example:
CELL1:
Airport1 Image Airport2 Image Airport3 >
CELL2:
Airport1 Image Airport2 Image Airport 3 Image Airport4 >
Etc.
The data come from a plist array with dictionary as follow
Root:
Flight1
Airport1:"kwi"
Airport2:"cai"
Airport3:"kwi"
Flight2
Airport1:"kwi"
Airport2:"bkk"
Airport3:"mnl"
Airport4:"bkk"
Airport5:"kwi"
Flight3
Airport1:"kwi"
Airport2:"jed"
Airport3:"kwi
So i need to make uilabels programatically with an uiimage inbetween each label horizontally!!
And here were i'm stuck..
I don't have much reputation to attach an image of the final result i want to accomplish..

How to link delegate and sourcedata to only one tableview in a set of tableviews created in the mainstoryboard

Please bear with me as I am completely new at objective-c. Thanks in advance for any help you can provide!
So here is basically what I am trying to accomplish: I have 3 main tables whose contents will never change, that I therefore chose to construct in the mainstoryboard. Think of these are different grouping drilled down step by step into more and more details. So you have:
Table 1 (higher level to table 2) > Table 2 (higher level to table 3) > Table 3
Now I need to add a 4th table, but whose contents will be changed, based on a CSV file. For now I am ignoring how use CSV files and there seems to be quite a bit of info on this already. So I am electing to use Arrays using (NSArray) to store and retrive the information.
I first build the prototype of this table in the mainstoryboard so that I have an idea of what it will look like. Then I wrote the code below which ideally will update the information in table 4:
VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
#end
VIEWCONTROLLER.M file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
NSArray *nominalManagers;
NSArray *tipsManagers;
NSArray *tipsAmt;
NSArray *nominalAmt;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tipsManagers = [[NSArray alloc]
initWithObjects:
#"SSG",
nil];
tipsAmt = [[NSArray alloc]
initWithObjects:
#"$tip",
nil];
nominalManagers = [[NSArray alloc]
initWithObjects:
#"Wel",
#"Gold",
#"Colch",
#"Stand",
nil];
nominalAmt = [[NSArray alloc]
initWithObjects:
#"$Wel",
#"$Gold",
#"$Colch",
#"$Stand",
nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
NSUInteger rowNum;
if (section == 0) {
rowNum = 1;
}
else if (section == 1) {
rowNum = 4;
}
else {
rowNum = 0;
}
return rowNum;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSUInteger row = [indexPath row];
NSInteger section = [indexPath section];
switch (section) {
case 0: // First cell in section 1
cell.textLabel.text = [tipsManagers objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [tipsAmt objectAtIndex:[indexPath row]];
break;
case 1: // Second cell in section 1
cell.textLabel.text = [nominalManagers objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [nominalAmt objectAtIndex:[indexPath row]];
break;
default:
cell.textLabel.text = #"WRONG SECTION";
break;
}
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
My main issue is that I am unable to connect the delegate and sourcedata of this class to Table 4 (the table whose information will change). Can you help? Do you have any suggestions to better accomplish my goal?
What I understand from your question is you do not have a uitableview to connect your data source and delegate !?
I am not sure if you have added 4th viewcontroller to your storyboard yet, If not just add a UItableviewcontroller to your storyboard, create a push segue then in identity inspector choose your VIEWCONTROLLER as class name.
Add IBOutlet UITableViewto your .h file like below and in your interface builder connect your datasource and delegate. When you need refresh your tableview call [self.tableviewname reloadData];
You can use NSMutableArray to edit the items in your array, if you use NSArray array items will be static.
VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet UITableView *fourthTable;
#end
VIEWCONTROLLER.M file
#implementation ViewController
#synthesize fourthTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tipsManagers = [[NSArray alloc]
initWithObjects:
#"SSG",
nil];
tipsAmt = [[NSArray alloc]
initWithObjects:
#"$tip",
nil];
nominalManagers = [[NSArray alloc]
initWithObjects:
#"Wel",
#"Gold",
#"Colch",
#"Stand",
nil];
nominalAmt = [[NSArray alloc]
initWithObjects:
#"$Wel",
#"$Gold",
#"$Colch",
#"$Stand",
nil];
[self.fourthTable reloadData];
}

Initialize UITableView with dynamic data

I am trying to display a list of friends in a UITableView.
I am loading the friends:
- (void)viewDidLoad
{
[super viewDidLoad];
[self apiGraphFriends];
}
Then I am setting my results in:
- (void)request:(FBRequest *)request didLoad:(id)result
{
friends = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *resultData = [result objectForKey:#"data"];
if ([resultData count] > 0) {
for (NSUInteger i=0; i<[resultData count]; i++) {
[friends addObject:[resultData objectAtIndex:i]];
}
} else {
//[self showMessage:#"You have no friends."];
}
}
and I am implementing the required UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [friends count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSManagedObjectModel *friend [fetch
FriendCell *cell = [tableView dequeueReusableCellWithIdentifier: #"friendCell"];
cell.cellName.text = [friends objectAtIndex:indexPath.row];
return cell;
}
The problem is that the method 'cellForRowAtIndexPath' is being called before my data has arrived, how do you prevent the automatic initialization of the table view and only initialyse it when you have data for it ?
The tableview is always loaded on launch. Instead, after the data has finished loading, call
[self.tableView reloadData];
This tells the tableView to refresh, and it calls cellForRowAtIndexPath and all that jazz again.
how do you prevent the automatic initialization of the table view
You don't. The table view will automatically try to load its data when it is created and displayed. But that should not be a problem.
and only initialyse it when you have data for it.
Send a reloadData message to the table view once the data is ready.
Have you remembered to change the number of sections from 0 to at least 1?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
To fully test this you can use the following code
- (void)viewDidLoad
{
[super viewDidLoad];
//Add your friend as you initialise the array
NSMutableArray *array = [NSMutableArray arrayWithObjects:f1,nil];
}
- (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.friends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//change this to whatever you need
NSString *friend = [self.friends objectAtIndex:indexPath.row];
cell.textLabel.text = friend;
return cell;
}
Your numberOfRowsInSection should return 0 if you don't have anything to display. After the data is loaded and ready to display, you should call reloadData on the tableView, and only then should the data be displayed. numberOfRowsInSection should now give the number of rows you loaded.
In short, you don't prevent the tableView from initializing. You initially tell it to display no data, and once your data is loaded, you tell it to display as many rows as you have.
Don't set the tableview delegate and datasource until AFTER you have your data.
i.e.
-(void)viewDidLoad{
[self apiGraphFriends];
self.tableview.delegate = self;
self.tableview.datasource = self;
}
I am assuming your method 'apiGraphFriends' doesn't use any background threading and isn't asynchronous. If it is, then just create a new method and put the datasource/delegate setting in there, then call it from a block in your apiGraphFriends method.
simply call reloadData of the tableView once you got your data, the tableview will reload its data and thus recall all your datasource methods to get its data.
You are so close!
- (void)viewDidLoad
{
[self apiGraphFriends];
[super viewDidLoad];
}
That should do it. If not, you can always call reloadData on the tableView.

Table view as an object in a view - how to initialize with an array of data?

My goal is to have something like a "select" option in a HTML form, but now in my app. After doing research it's probably best to do this with a table view. I though of the picker view, but the fixed height is too big.
With the interface builder I simply placed a table view on my subclass of UIViewController.
How do I fill the Table view with options? I've seen many tutorials, but those are all for having a UITableView as their own class and filling up the entire screen. In my application this is just a small piece of the entire form.
What a nightmare to create a relatively simple thing like a table view. It either crashes or I get a table view that covers my entire view and that is not filled with anything.
The variable countryTable is connected to the object in the interface builder.
Frustrated after a hard day of work. Anyone got the complete working code? That would be great. I already had a great look at apple's explenation AND various tutorials, but I can't figure it out.
I've tried multiple things, but this is my current code:
#interface myView: UIViewController
{
NSArray *countryArray;
IBOutlet UITableView * countryTable;
}
#property (nonatomic, retain) UITableView *countryTabel;
#end
and in my .m file
#implementation myView
#synthesize countryTable;
- (void)loadView
{
self.countryTable.dataSource = self;
}
- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc] initWithObjects:#"test1", #"test2",
#"test3",nil];
self.countryTable = array;
[array release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void) dealloc
{
[countryTable release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.countryTable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [countryTable objectAtIndex:row];
return cell;
}
#end
You are on the right track. If you were to use a UITableViewController subclass, you would obviously have a full screen table view by default. Going the route of using a UIViewController subclass with a UITableView as a subview in the UIViewController's view is the right way to go. A few things that you will need to address are:
1) In the UIViewControllers header file you will need to add <UITableViewDatasource, UITableViewDelegate> as your view controller is responsible for fill implementing this functionally.
2) In viewDidLoad:, set self.contryTable.delegate = self; and self.countryTable.datasource = self;
The following protocol methods need to be implemented like so:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return countryArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [countryArray objectAtIndex:row];
return cell;
}
Hope this helps.

3 tables on ipad with different data

Im trying to implement 3 different table views that load 3 different arrays of information,
In some forum I read that I could use the .tag to differentiate the tables and use conditionals to load the data,
I tried
changing the name of the table
in identity /name of the xib to my table,
and use
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
//---try to get a reusable cell---
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//---create new cell if no reusable cell is available---
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
/*
//---set the text to display for the cell---
NSString *cellValue = [listOfMovies objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue; */
if (tableView.tag == 1) {
//Deal with table 1 - contains 5 sections
cell.textLabel.text = [array objectAtIndex:indexPath.row];
} else if (tableView.tag == 2) {
//Deal with table 2 - contains 1 section
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
}
// cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
else {
cell.textLabel.text = [array objectAtIndex:indexPath.row];
}
return cell;
}
to change the cell content to show the other array
but is not working (long shot!)
so, How to define this .tag for my tables
also, I want the same for row (count) and sections 1
for all the tables,
so shall I just leave them like that??
I have the 3 tables showing the same array at the moment,
thank you so much!
Um, haven't tried this, but I'd be inclined to look into associating each table view with it's own unique UITableViewDataSource instance. That way they do not have to be all coded in the same class and everything becomes simpler. Not sure how you would go about assembling this, but it should be too hard to work out I would think.
I think you're over-complicating things. In one of my apps I have one table view controller for three tables. The interface definition is something like:
#interface TableViewController : UITableViewController <UIGestureRecognizerDelegate> {
IBOutlet UITableView *leftTable;
IBOutlet UITableView *centerTable;
IBOutlet UITableView *rightTable;
...
Then, in each of the delegate methods, I check to see which table it is before performing the relevant action or returning the relevant result. For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == leftTable) {
return [self.leftItems count];
} else if (tableView == centerTable) {
return [self.centerItems count];
} else {
return [self.rightItems count];
}
}