xcode创建具有相同内容的2个UITableView

时间:2014-01-30 11:35:00

标签: ios objective-c xcode uitableview

我正在尝试使用与其他UITableView相同的值创建另一个UITableView。 这是我到目前为止所拥有的。

第一个UITableView工作得很好,它可以显示我想要的数据。然而,另一个根本没有显示任何东西。只是空单元格。 cell2应该用于另一个表。

- (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;
}

如何解决这个问题以及究竟是什么问题?

1 个答案:

答案 0 :(得分:1)

你需要发起&&根据您的tableView 返回单元格。

这应该有效:

- (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;
}

请告诉我,如果您有任何问题。