【问题标题】:xcode 4.5 iOS Storyboard and UITableView delegate confusionxcode 4.5 iOS Storyboard 和 UITableView 委托混淆
【发布时间】:2013-01-25 12:06:23
【问题描述】:

我正在尝试制作一个简单的 iOS 应用程序,它使用 UINavigationController 两个 ViewController 和一个 UITableView 来显示列表。问题是该表拒绝显示来自 NSArray 的数据。如图所示,所有代表都已设置完毕。

如果我为 ViewController 创建一个 xib 文件,一切都会正常工作,但如果我可以在 Storyboard 中再拖动一个 ViewController 并将它的类链接到我的 ItemListViewController,我不需要一个 xib 文件,如第一张图片所示。

ItemListViewController.h

#进口 @interface ItemListViewController : UIViewController{ NSMutableArray *tblDataSource; IBOutlet UITableView *tblSimpleTable; } @property (assign) NSMutableArray *tblDataSource; @property(保留)IBOutlet UITableView *tblSimpleTable; @结尾

ItemListViewController.m

#import "ItemListViewController.h" @interface 项目列表视图控制器 () @结尾 @implementation ItemListViewController @synthesize tblDataSource,tblSimpleTable; - (id)initWithNibName:(NSString *)nibNameOrNil 包:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 如果(自我){ // 自定义初始化 } 回归自我; } - (void)viewDidLoad { [超级视图DidLoad]; // 加载视图后做任何额外的设置。 UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg.png"]]; self.tblSimpleTable.backgroundColor = 背景; [背景发布]; NSArray * data = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut ”,@“星巴克咖啡”,@“蔬菜咖喱”,@“鸡蛋方便面”,@“叉烧面”,@“日本猪肉面”,@“绿茶”,@“泰式虾饼” ,@“愤怒的小鸟蛋糕”,@“火腿芝士帕尼尼”,无]; [tblDataSource setArray:数据]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // 处理所有可以重新创建的资源。 } #pragma mark - 表格视图数据源 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回部分的数量。 返回0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回节中的行数。 NSLog(@"@%d",[tblDataSource 计数]); 返回 [tblDataSource 计数]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(细胞 == 零){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // 配置单元格... UIImage *placeholder = [UIImage imageNamed:@"icon.png"]; [cell.imageView setImage:placeholder]; cell.textLabel.text = [tblDataSource objectAtIndex:indexPath.row]; 返回单元格; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"点击行@%d",indexPath.row); } @结尾

如果有人能指出我正确的方向,那我做错了什么。

【问题讨论】:

    标签: ios uitableview delegates datasource


    【解决方案1】:

    numberOfSectionsInTableView 中,您返回 0,表示没有要生成的部分(因此也没有行)。您可能想要返回 1。

    另外,你想确保你保留你的NSMutableArray,所以你真的很想做:

    @property (nonatomic, retain) NSMutableArray *tblDataSource;
    

    【讨论】:

    • 我返回 1,但表格仍然没有显示任何内容。
    • 您的回答是正确的 :) 问题也是我在数据源数组中添加元素的方式 :) [tblDataSource setArray:data];不工作,所以我把它改成了 tblDataSource = [[NSMutableArray alloc] initWithObjects:@"Egg Benedict", @"Mushroom Risotto",nil];它有效:D 感谢您为我指出正确的方向并帮助我调试它。
    • @Constantine 顺便说一句,与您所做的相同,您可以将属性更改为retain(并保留您创建的数组)。它可能是等效的,但使正确的内存管理更加明确。另外,(因为你没有使用 ARC)不要忘记定义你的 dealloc 方法来释放 tblDataSource,所以当你完成时数组将被清理。
    • 而且,就风格而言,当您@synthesize 变量时,不再建议您也显式声明类实例变量。 @synthesize 为您执行此操作,并且通过将其留给编译器,您可以避免出现可能给您留下无关 ivars 的拼写错误。
    • 感谢@Rob 提供的所有这些提示 :) 内存管理是我在尝试使其工作时考虑的最后一部分 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多