HGo*_*mez 5 iphone xcode objective-c uitableview ios5
我正在尝试为我的UITableView创建一个自定义单元格.我正在使用Xcode 4.2并使用故事板和ARC.
我创建了一个类来表示自定义单元格,如下所示:
ResultsCustomCell.h
#import <UIKit/UIKit.h>
@interface ResultsCustomCell : UITableViewCell
{
IBOutlet UILabel *customLabel;
}
@property (nonatomic, retain) IBOutlet UILabel* customLabel;
@end
Run Code Online (Sandbox Code Playgroud)
ResultsCustomCell.m
#import "ResultsCustomCell.h"
@implementation ResultsCustomCell
@synthesize customLabel;
@end
Run Code Online (Sandbox Code Playgroud)
然后我在视图控制器中实现了UITableView方法,如下所示: ViewController.m
#import "ViewController.h"
#import "ResultsCustomCell.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
myCell.customLabel.text = @"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
@end
Run Code Online (Sandbox Code Playgroud)
应用程序构建成功但随后崩溃并给我以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Run Code Online (Sandbox Code Playgroud)
我错过了哪一部分?
sum*_*ome 21
这是一个令人恼火的问题,因为调试器不会让您知道发生了什么.它将在异常中中断,然后在单击Continue Execution时关闭.
这让我疯狂了30分钟,直到我右键点击自定义单元格中的UILabel给我提出问题,这揭示了答案:你单元格中的一个控件已经(或者,当你明显修复它时,可能没有注意到一个虚假的出路."虚假插座"是指连接到您的类中不再存在的IBOutlet属性的插件.在我的情况下,我更改了属性的名称并重新连接,但忘记删除旧的引用插座连接.
一旦我移除了有问题的插座,问题就消失了.
我不完全确定我做了什么来解决我的问题,但它现在工作正常。
这是我目前在 ViewController.m 中的内容
注意:ResultsCustomCell.h 和 .m 与上面相同。
#import "ViewController.h"
#import "ResultsCustomCell.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
// Make sure there are no quotations (") around the cell Identifier.
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
myCell.customLabel.text = @"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
@end
Run Code Online (Sandbox Code Playgroud)
然后我确保以下内容正确:
使用此代码并检查上述所有内容后,它似乎可以工作。
| 归档时间: |
|
| 查看次数: |
5012 次 |
| 最近记录: |