0

我在细节和主控中使用 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 教程得到了这个。任何帮助表示赞赏!

4

2 回答 2

1

在情节提要中选择原型单元格,转到属性检查器,将其输入到标识符字段中。并设置正确的标识符。
信用: http ://www.raywenderlich.com/forums/viewtopic.php?f=13&t=4217

于 2012-12-04T14:10:36.467 回答
0

在本次通话中:

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

您忘记测试单元格是否为零,而是像这样分配它:

if( cell == nil  {
   cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                  reuseIdentifier: cellIdentifier ] autorelease];
}
于 2013-04-17T20:57:59.773 回答