2013-04-13 33 views
0

作爲示例,我試圖將地名加載到我的頂部UITableView中,名爲myTableView。在底部,我試圖僅顯示「test2」。如何在同一視圖上的2個UITableView之間拆分數據

當然,所有的數據都被加載到每個UITableView。如果我滾動到myTableView2的底部,就會顯示「test2」這個詞,但我希望它是唯一的東西。

有沒有辦法,也許通過委託不同的數據源,我可以分開我wan在每個UITableView?我試圖用if語句分開,但很快意識到這沒有效果。

在我看來,我可能需要設置一個單獨的數據源爲myTableView2,但我不知道該怎麼做......

代碼是在這裏:

- (void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    [self setString]; 
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 

    NSString *string1 = [dic objectForKey:@"name"]; 

    for (NSDictionary *diction in dic) 
    { 
     [array addObject:string1]; 
    } 

    [[self myTableView] reloadData]; 

    NSString *string2 = [NSString stringWithFormat:@"test2"]; 

    [array addObject:string2]; 

    [[self myTableView2] reloadData]; 

    [indicator stopAnimating]; 
    indicator.hidden=YES; 
    [indicator removeFromSuperview]; 
    ilabel.hidden = YES; 
} 

myTableView on top, myTableView2 on bottom

回答

0

用以下代碼解決。關鍵是要有兩個不同的陣列來加載相應的數據,並將其顯示在底部,以便能夠區分具有IF語句的UITableViews:

- (void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
[self setString]; 
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 
NSString *string1 = [dic objectForKey:@"name"]; 
NSString *string2 = [NSString stringWithFormat:@"test2"]; 

    for (NSDictionary *diction in dic) 
    { 
     [array addObject:string1]; 
     [array2 addObject:string2]; 

     [[self myTableView] reloadData]; 
     [[self myTableView2] reloadData]; 
    } 

    [indicator stopAnimating]; 
    indicator.hidden=YES; 
    [indicator removeFromSuperview]; 
    ilabel.hidden = YES; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if(!cell) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

if (tableView == self.myTableView) 
{ 
    if ([[array objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]]) 
    {   
    cell.textLabel.text = [[array objectAtIndex:indexPath.row] stringValue]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
    else 
    {   
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
} 
if (tableView == self.myTableView2) 
{ 
    if ([[array2 objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]]) 
    { 
     cell.textLabel.text = [[array2 objectAtIndex:indexPath.row] stringValue]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
    else 
    { 
     cell.textLabel.text = [array2 objectAtIndex:indexPath.row]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
} 
return cell; 
} 
相關問題