I am trying to add 2 UITableView‘s to my UIViewController. I need to add different data to these tables.
This is how i added the 2 tables (this code was added to the ViewDidLoad method)
self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0,140,292,250) style:UITableViewStylePlain] ;
self.tableView2 .dataSource = self;
self.tableView2 .delegate = self;
Then the other table
self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,100) style:UITableViewStylePlain] ;
self.tableView1 .dataSource = self;
self.tableView1 .delegate = self;
The number of sections defined as follows;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==tableView1) {
return 12;
}
else { return 10; }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...... more code here
if (tableView == self.tableView1) {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text=@"Cells .... ";
}
else{
// the remaining code here.. i am populating the cell as in the previous `IF` condition.
}
}
The problem is that, i am getting only the first Table fill and not the 2nd table. Why is this? and how can i solve this ?
EDIT:
I also added the following code, hope it makes a change
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView==tableView1) {
return 1;
}
else if (tableView==tableView2) { return 0; }
else { return 0; }
}
Try to follow these steps in order to make the two table views having the same
delegateanddataSource.Set the
tagproperty of your table views, and#defineconstants on the two values. This makes the code consistent.In delegate and datasource methods you implement in your view controller subclass, test the
tagproperty value against the constants you defined.Don’t return 0 section for a table view, it won’t display any cell at all.
So, for example :