当我加载了第二个UITableViewController时,无法用数组填充单元格

时间:2010-04-01 15:41:06

标签: iphone objective-c uitableview

我对iPhone编程很新,我正在创建我的第一个应用程序,(世界杯一个) 第一个视图是表视图。单元格文本标签用数组填充,所以它显示所有组(组a,B,c等)然后当你选择一个组时,它会拉上另一个UITableViewcontroller,但无论我做什么我都不能设置文本标签细胞(例如法国,墨西哥,南非等)实际上我对cellForRowAtIndexPath所做的一切都有所作为,有人可以告诉我我做错了吗

由于

这是我的视图控制器代码:

GroupADetailViewController.m

#import "GroupADetailViewController.h"

@implementation GroupADetailViewController

@synthesize groupLabel = _groupLabel;
@synthesize groupADetail = _groupADetail;
@synthesize teamsInGroupA;

#pragma mark Memory management
- (void)dealloc {
    [_groupADetail release];
    [_groupLabel release];
    [super dealloc];
}

#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the number label to show the number data
    teamsInGroupA  = [[NSArray alloc]initWithObjects:@"France",@"Mexico",@"Uruguay",@"South Africa",nil];
    NSLog(@"loaded");
    // Set the title to also show the number data
    [[self navigationItem]setTitle:@"Group A"];

    //[[self navigationItem]cell.textLabel.text:@"test"];

    //[[self navigationItem] setTitle[NSString String
}

- (void)viewDidUnload {
    [self setgroupLabel:nil];
}

#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    // Return the number of sections in the table view
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in a specific section
    // Since we only have one section, just return the number of rows in the table
    return 4;
    NSLog:("count is %d",[teamsInGroupA count]);
}

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

    // Reuse an existing cell if one is available for reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];

    // If no cell was available, create a new one
    if (cell == nil) {
        NSLog(@"no cell, creating");
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    NSLog(@"cell already there");

    // Configure the cell to show the data for this row
     //[[cell textLabel]setText:[NSString string

    //[[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    //NSUInteger row = [indexPath row];


    //[cell setText:[[teamsInGroupA objectAtIndex:indexPath:row]retain]];
    //cell.textLabel.text:@"Test"   
    [[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    return cell;
}


@end

GroupADetailViewController.m

#import "GroupADetailViewController.h"

@implementation GroupADetailViewController

@synthesize groupLabel = _groupLabel;
@synthesize groupADetail = _groupADetail;
@synthesize teamsInGroupA;

#pragma mark Memory management
- (void)dealloc {
    [_groupADetail release];
    [_groupLabel release];
    [super dealloc];
}

#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the number label to show the number data
    teamsInGroupA  = [[NSArray alloc]initWithObjects:@"France",@"Mexico",@"Uruguay",@"South Africa",nil];
    NSLog(@"loaded");
      // Set the title to also show the number data
    [[self navigationItem]setTitle:@"Group A"];

    //[[self navigationItem]cell.textLabel.text:@"test"];

    //[[self navigationItem] setTitle[NSString String
}

- (void)viewDidUnload {
    [self setgroupLabel:nil];
}

#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    // Return the number of sections in the table view
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in a specific section
    // Since we only have one section, just return the number of rows in the table
    return 4;
    NSLog:("count is %d",[teamsInGroupA count]);
}

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

    // Reuse an existing cell if one is available for reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];

    // If no cell was available, create a new one
    if (cell == nil) {
        NSLog(@"no cell, creating");
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    NSLog(@"cell already there");

    // Configure the cell to show the data for this row
    //[[cell textLabel]setText:[NSString string

    //[[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    //NSUInteger row = [indexPath row];


    //[cell setText:[[teamsInGroupA objectAtIndex:indexPath:row]retain]];
    //cell.textLabel.text:@"Test"   
    [[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    return cell;
}

@end

3 个答案:

答案 0 :(得分:0)

您是否在第二个UITableView上正确设置了委托?它是从xib加载还是只是硬编码?另外,我没有看到didSelectCellForIndexPath:在任何地方。第二个tableView是否在屏幕上加载?

答案 1 :(得分:0)

您的tableViewController必须将delegate属性设置为符合UITableViewDelegate的实例,并将dataSource属性设置为符合UITableViewDataSource协议的对象。当然,这些属性可以设置为self: 你可以插入这个

self.dataSource = self;
self.delegate = self;
在viewDidLoad方法中

如果不这样做,则永远不会调用您实现的必需方法(在协议中定义),因为默认情况下这两个属性为nil。 它能解决你的问题吗?

答案 2 :(得分:0)

这个cocoapod https://github.com/xmartlabs/XLData使得UITableView / UICollectionView的加载比直接处理dataSource和delegates更容易。