priya sharma priya sharma - 4 months ago 17
Objective-C Question

two table views in one UIviewcontroller

I have created a page where i need to use two table views for adding dynamic options. The problem is that only one table is displaying its data and the other one is not. Am totally confused where did i do wrong.

Here is the screenshot of my screen

table displaying data & table not displaying data

Below am adding my code-

@interface FlirtMatchViewController ()
{
UILabel *headerTitle;
NSArray *tableData;
NSArray *arrimages;

NSArray *tableData1;
NSArray *arrimages1;



}

@end

@implementation FlirtMatchViewController

- (void)viewDidLoad {
[super viewDidLoad];

tableData = [NSArray arrayWithObjects:@"TattooSingles",@"Eyecatcher",@"Tattoo Toplist",@"Radar",@"Flirt Chat",@"Free VIP Membership",@"VIP Membership",@"Invite Friends",nil];

arrimages = [NSArray arrayWithObjects:@"tatto_single@2x.png",@"eye_catcher.png",@"tatto_toplist.png",@"radar.png",@"Flirt_chat.png",@"free_vip.png",@"vip_membership_navigation.png",@"add-friend1.png", nil];



tableData1 = [NSArray arrayWithObjects:@"TattooSingles",@"Eyecatcher",@"Tattoo Toplist",@"Radar",@"Flirt Chat",@"Free VIP Membership",@"VIP Membership",@"Invite Friends",nil];

arrimages1 = [NSArray arrayWithObjects:@"tatto_single@2x.png",@"eye_catcher.png",@"tatto_toplist.png",@"radar.png",@"Flirt_chat.png",@"free_vip.png",@"vip_membership_navigation.png",@"add-friend1.png", nil];



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
// return [tableData count];
//}
{
if(tableView==_leftTableView)
{
return [tableData count];
}
else {
return [tableData1 count];
}
}

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

{

static NSString *CellIdentifier2 = @"Cell2";
static NSString *CellIdentifier1 = @"Cell1";

if(tableView==self.leftTableView)
{
static NSString *CellIdentifier1 = @"Cell1";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

return cell;
}
else
{
static NSString *CellIdentifier2 = @"Cell2";

UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell1 == nil)
{
cell1= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}

cell1.textLabel.text = [tableData1 objectAtIndex:indexPath.row];
return cell1;
}

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:@"TattooSinglesScreen"];


EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:@"EyecatcherScreen"];


TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:@"TattooToplist"];


int i=indexPath.row;

if(i==0){
[self.navigationController pushViewController:obj animated:NO];
}
else if (i==1) {
[self.navigationController pushViewController:obj1 animated:NO];
}
else if (i==2) {
[self.navigationController pushViewController:obj2 animated:NO];
}

}


Also am trying to navigate the first three options to their particular screen by using the code below. But the code is not working for me. Can anyone clear my doubts?

Answer

do like

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self. leftTableView)
{
    return [tableData count];
}

if (tableView == self. rightTableView)
{
    return [tableData1 count];
}


}


-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{    
if(tableView==self.leftTableView)
{
    static NSString *CellIdentifier1 = @"Cell1";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
    }

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    return cell;
}

if (tableView == self._rightTableView)
{
    static NSString *CellIdentifier2 = @"Cell2";

    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    if (cell1 == nil)
    {
        cell1= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
    }

    cell1.textLabel.text = [tableData1 objectAtIndex:indexPath.row];
    return cell1;
}

}
Comments