NSInternalInconsistencyException错误UITableViewController

时间:2012-10-06 20:53:37

标签: uitableview

我正在使用Detail和Master中的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教程来了解这一点。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:1)

在故事板中选择原型单元格,转到“属性”检查器,在“标识符”字段中键入它。 并设置正确的标识符。
信用: http://www.raywenderlich.com/forums/viewtopic.php?f=13&t=4217

答案 1 :(得分:0)

在此电话会议中:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell"];

你忘了测试单元格为零,而是像这样分配它:

if( cell == nil  {
   cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                  reuseIdentifier: cellIdentifier ] autorelease];
}