连接2个或多个tableviews以显示来自同一nsarray的信息

时间:2012-11-21 10:15:14

标签: 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

提前大大提高。

1 个答案:

答案 0 :(得分: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中显示任何内容?确保已连接dataSource和delegate。

希望有帮助=)

相关问题