ibab ibab - 1 month ago 8
iOS Question

xcode Creating 2 UITableViews with same content

I am trying to create another UITableView with the same values of the other UITableView.
Here is what I have so far.

The first UITableView works just fine, it displays the data as I want. The other one however, does not display anything at all. Just empty cells. The cell2 is supposed to be for the other table.

- (void)viewDidLoad
{
[super viewDidLoad];

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]]];

tableData = [[NSArray alloc] initWithObjects:@"Location 1", @"Location 2", @"Location 3", @"Location 4", @"Location 5", @"Location 6", @"Location 7", @"Location 8", @"Location 9", @"Location 10", @"Location 11", @"Location 12", @"Location 13", @"Location 14", nil];

mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

#pragma mark - TableView Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = nil;
UITableViewCell * cell2 = nil;

cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
cell2 = [tableView dequeueReusableCellWithIdentifier:@"Listaus"];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyCell"];
}

if (cell2 == nil) {
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Listaus"];
}

cell.textLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.textColor=[UIColor whiteColor];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [mainDelegate.distances objectAtIndex:indexPath.row];
cell2.textLabel.text = [tableData objectAtIndex:indexPath.row];

return cell;
}


How to fix this and what exactly is wrong?

Answer

You need to initiate && return the cell according to your tableView .

This should work :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = nil;
    if(tableView == tableView1){
        cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
        if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyCell"];
        }

    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"Listaus"];
        if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Listaus"];
        }
    }
     cell.textLabel.textColor=[UIColor whiteColor];
    cell.detailTextLabel.textColor=[UIColor whiteColor];
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [mainDelegate.distances objectAtIndex:indexPath.row];

    return cell;
}

Let me know, If you have any issues with it.

Comments