2012-11-21 41 views
1

我很困惑,并尝试了很多不同的东西来消除我面临的错误。连接2个或更多的tableviews显示来自同一个nsarray的信息

我有一个NSArray的蛋白质,在我的故事板中我有两个独立的表格视图,我希望能够显示protiens数组。

我在 - (UITableViewcell *)和@end中有两个错误。

如果有人可以帮助请找我下面的代码ViewController.m:(请忽略接近尾声SEGUE编码)

#import "JViewController.h" 
#import "OneViewController.h" 
#import "TwoViewController.h" 

@interface JViewController() 

@end 

@implementation JViewController 
{ 
    NSArray *proteins; 
} 

@synthesize tableView1; 
@synthesize tableView2; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    proteins = [NSArray arrayWithObjects:@"Chicken", @"Turkey", @"Ham", @"Steak", @"Pork Chop",  @"Roast Lamb", @"Salmon", @"Egg", @"Lentils", @"Kidney Beans", nil]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == tableView1) { 
     return [proteins count]; 
    { 
    if (tableView == tableView2) { 
     return [proteins count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ProteinCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    if (tableView1 == tableView2) 
    { 
     proteins; 
    } 
    else 
    { 
     proteins; 
    } 

    cell.textLabel.text = [proteins objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showMealOneDetail"]) 
    { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    OneViewController *destViewController = segue.destinationViewController; 
    destViewController.proteinName = [proteins objectAtIndex:indexPath.row]; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

大三江源提前。

回答

0

您这里有两个开放的支架,关闭此一个:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == tableView1) { 
     return [proteins count]; 
    ->> { 
    if (tableView == tableView2) { 
     return [proteins count]; 
    } 
} 

而且你是什么意思与此?

if (tableView1 == tableView2) 
{ 
    proteins; 
} 
else 
{ 
    proteins; 
} 

试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ProteinCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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


    cell.textLabel.text = [proteins objectAtIndex:indexPath.row]; 
    return cell; 
} 

你已经能够在一个tableview中表现出什么?确保你有连接的数据源和委托。

希望它有帮助=)

相关问题