我在细节和主控中使用 TableViewController 做一个 SplitViewController。
当我尝试运行时,我在控制台中收到此消息:
2012-10-06 16:42:05.304 BaylorNotes7[3040:c07] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:5471
2012-10-06 16:42:05.306 BaylorNotes7[3040:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
问题出在 DetailViewController.m 中:
#import "DetailViewController.h"
#import "NSString+HTML.h"
typedef enum { SectionHeader, SectionDetail } Sections;
typedef enum { SectionHeaderTitle, SectionHeaderDate, SectionHeaderURL } HeaderRows;
typedef enum { SectionDetailSummary } DetailRows;
@implementation DetailTableViewController
@synthesize item, dateString, summaryString;
#pragma mark -
#pragma mark Initialization
- (id)initWithStyle:(UITableViewStyle)style {
if ((self = [super initWithStyle:style])) {
}
return self;
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
// Super
[super viewDidLoad];
// Summary
if (item.summary) {
self.summaryString = [item.summary stringByConvertingHTMLToPlainText];
} else {
self.summaryString = @"[No Summary]";
}
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0: return 1;
default: return 1;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"DetailCell"];
// Display
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:15];
if (item) {
// Item Info
NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
// Display
switch (indexPath.section) {
case SectionHeader: {
// Header
switch (indexPath.row) {
case SectionHeaderTitle:
cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:20];
cell.textLabel.text = itemTitle;
break;
}
break;
}
case SectionDetail: {
// Summary
cell.textLabel.text = summaryString;
cell.textLabel.numberOfLines = 100; // Multiline
cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:20];
break;
}
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == SectionHeader) {
// Regular
return 60;
} else {
// Get height of summary
NSString *summary = @"[No Summary]";
if (summaryString) summary = summaryString;
CGSize s = [summary sizeWithFont:[UIFont systemFontOfSize:20]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT) // - 40 For cell padding
lineBreakMode:UILineBreakModeWordWrap];
return s.height; // Add padding
}
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Open URL
if (indexPath.section == SectionHeader && indexPath.row == SectionHeaderURL) {
if (item.link) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:item.link]];
}
}
// Deselect
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
我很确定问题出在 Table 声明中的某个地方。如果有人想知道,我按照 MWFeedParser 教程得到了这个。任何帮助表示赞赏!