2014-01-30 49 views
-2

我想創建另一個UITableView與其他UITableView相同的值。 這是我到目前爲止。Xcode創建2個具有相同內容的UITableViews

第一個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; 
} 

如何解決這個問題,究竟是什麼錯誤?

+0

你可以發佈你的兩個tableView的「init」代碼嗎? – MathieuF

+1

假設'UITableView'委託('委託'和'datasource')被設置爲相同的視圖控制器,您需要確定哪個表視圖要求單元格,爲該表創建單元格並返回該單元格。 – bobnoble

回答

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

讓我知道,如果你有任何問題了。

+0

我應該在哪裏聲明tableView 1? – ibab

+0

你應該決定。無論如何,你有正確的tableViews。 tableView1指出你聲明的tableView Name(IBOutlet或編程)。 –

相關問題