【问题标题】:Table View Cell error when Identifier is set设置标识符时的表视图单元格错误
【发布时间】:2012-04-28 04:13:57
【问题描述】:

在 xCode 4.2 中,当我将 identifier 属性设置为情节提要中的原型单元格时,我在尝试构建项目时收到错误 Uncategorized - Compilation Failed

我不确定您需要哪些信息,所以我会提供我认为相关的信息...

首先,我有一个视图控制器,我在 Storyboard 中设置了一个表格视图。在表格视图中,我放置了一个原型单元格。在原型单元格中,我放置了一个 UIImageView 和 3 个标签。

我有一个名为 DOR_SearchCustomCell .h 和 .m 文件的自定义单元格类。这是.h文件:

@interface DOR_SearchCustomCell : UITableViewCell {

    __weak IBOutlet UIImageView *cellImage;
    __weak IBOutlet UILabel *cellTitle;
    __weak IBOutlet UILabel *cellText;
    __weak IBOutlet UILabel *cellDistance;
}

@property (weak, nonatomic) IBOutlet UIImageView *cellImage;
@property (weak, nonatomic) IBOutlet UILabel *cellTitle;
@property (weak, nonatomic) IBOutlet UILabel *cellText;
@property (weak, nonatomic) IBOutlet UILabel *cellDistance;

@end

还有.m文件:

#import "DOR_SearchCustomCell.h"

@implementation DOR_SearchCustomCell

@synthesize cellImage;
@synthesize cellTitle;
@synthesize cellText;
@synthesize cellDistance;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

我的原型单元格将此类设置为自定义类,样式设置为自定义,标识符设置为specialsCell,并且图像和标签与类文件中的变量相关联。

控制视图控制器的类是DOR_SearchViewController,带有.h 和.m 文件。 .h 文件是:

#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "DOR_RestaurantClass.h"
#import "DOR_SearchCustomCell.h"

@class Reachability;

@interface DOR_SearchViewController : UIViewController <UIActionSheetDelegate, NSURLProtocolClient, NSXMLParserDelegate, UITableViewDelegate, UITableViewDataSource> {

    __weak IBOutlet UITableView *tblView;

    NSMutableData *receivedData;
    NSXMLParser* parser;
    NSString *currentElement;
    NSMutableString *currentElementValue;
    NSMutableArray *listItems;
    NSMutableArray *listOfIds;
    NSMutableArray *listOfNames;
    NSMutableArray *listOfDistances;
    NSMutableArray *listOfImages;
    NSMutableArray *listOfAddresses;
    NSMutableArray *listOfAddresses2;
    NSMutableArray *listOfAddresses3;
    NSMutableArray *listOfDescriptions;
    NSMutableArray *listOfPhones;
}

@property (weak, nonatomic) IBOutlet UITableView *tblView;

@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSXMLParser *parser;
@property (nonatomic, retain) NSString *currentElement;
@property (nonatomic, retain) NSMutableString *currentElementValue;
@property (nonatomic, retain) NSMutableArray *listItems;
@property (nonatomic, retain) NSMutableArray *listOfIds;
@property (nonatomic, retain) NSMutableArray *listOfNames;
@property (nonatomic, retain) NSMutableArray *listOfDistances;
@property (nonatomic, retain) NSMutableArray *listOfImages;
@property (nonatomic, retain) NSMutableArray *listOfAddresses;
@property (nonatomic, retain) NSMutableArray *listOfAddresses2;
@property (nonatomic, retain) NSMutableArray *listOfAddresses3;
@property (nonatomic, retain) NSMutableArray *listOfDescriptions;
@property (nonatomic, retain) NSMutableArray *listOfPhones;

- (void)get_table_data;

@end

还有.m文件:

#import "DOR_SearchViewController.h"
#import "Reachability.h"

@implementation DOR_SearchViewController

@synthesize tblView;

@synthesize receivedData;
@synthesize parser;
@synthesize currentElement;
@synthesize currentElementValue;
@synthesize listItems;
@synthesize listOfIds;
@synthesize listOfNames;
@synthesize listOfDistances;
@synthesize listOfImages;
@synthesize listOfAddresses;
@synthesize listOfAddresses2;
@synthesize listOfAddresses3;
@synthesize listOfDescriptions;
@synthesize listOfPhones;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (![[self loadSettings:@"view"] isEqualToString: @""]){
        [sortButton setTitle:[self loadSettings:@"view"] forState: (UIControlState)UIControlStateNormal];
    }

    listItems = [[NSMutableArray alloc] init];
    listOfIds = [[NSMutableArray alloc] init];
    listOfNames = [[NSMutableArray alloc] init];
    listOfDistances = [[NSMutableArray alloc] init];
    listOfImages = [[NSMutableArray alloc] init];
    listOfAddresses = [[NSMutableArray alloc] init];
    listOfAddresses2 = [[NSMutableArray alloc] init];
    listOfAddresses3 = [[NSMutableArray alloc] init];
    listOfDescriptions = [[NSMutableArray alloc] init];
    listOfPhones = [[NSMutableArray alloc] init];
    [self get_table_data];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)get_table_data {
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.url.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        //NSLog (@"At connection");
        receivedData = [NSMutableData data];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {  
    if(parser){
        parser = nil;
    }

    parser = [[NSXMLParser alloc] initWithData: receivedData];
    [parser setDelegate: self];
    [parser setShouldResolveExternalEntities: YES];
    [parser parse];

    [tblView reloadData];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    //NSLog (@"At parser");
    currentElement = elementName;

    if ([currentElement isEqualToString:@"restaurant_details"]) {
        if ([currentElement isEqualToString:@"total_results"]) {
            //NSLog(@"Element: %@", currentElement);
        }else if ([currentElement isEqualToString:@"restaurant"]) {
            restaurantObj = [[DOR_RestaurantClass alloc]init];
        }
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    //NSLog (@"At parser2");
    if(!currentElementValue)
        currentElementValue=[[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    //NSLog(@"Name to be saved in Array :- %@",currentElement);

    //NSLog(@"the parser just found this text in a tag:%@",currentElementValue);
    if([currentElement isEqualToString:@"name"]) {
        restaurantObj.name=[NSString stringWithFormat:@"%@",currentElementValue];
        [listOfNames addObject:currentElementValue];
        //NSLog(@"At Name | Array = %@", listOfNames);
    }else{
        restaurantObj.name=@"";
    }
    if([currentElement isEqualToString:@"distance_from_current_location"]) {
        restaurantObj.distance=[NSString stringWithFormat:@"%@",currentElementValue];
        [listOfDistances addObject:string];
    }else{
        restaurantObj.distance=@"";
    }
    if([currentElement isEqualToString:@"restaurant_id"]) {
        restaurantObj.restId=[NSString stringWithFormat:@"%@",currentElementValue];
        [listOfIds addObject:string];
    }else{
        restaurantObj.restId=@"";
    }
    if([currentElement isEqualToString:@"address"]) {
        NSArray *stringArray = [currentElementValue componentsSeparatedByString:@" | "];
        restaurantObj.address=[stringArray objectAtIndex:0];
        restaurantObj.address2=[stringArray objectAtIndex:1];
        restaurantObj.address3=[stringArray objectAtIndex:2];
        [listOfAddresses addObject:[stringArray objectAtIndex:0]];
        [listOfAddresses2 addObject:[stringArray objectAtIndex:1]];
        [listOfAddresses3 addObject:[stringArray objectAtIndex:2]];
    }else{
        restaurantObj.address=@"";
        restaurantObj.address2=@"";
        restaurantObj.address3=@"";
    }
    if([currentElement isEqualToString:@"phone_number"]) {
        restaurantObj.phone=[NSString stringWithFormat:@"%@",currentElementValue];
        [listOfPhones addObject:string];
    }else{
        restaurantObj.phone=@"";
    }
    if([currentElement isEqualToString:@"description"]) {
        restaurantObj.description=[NSString stringWithFormat:@"%@",currentElementValue];
        [listOfDescriptions addObject:string];
    }else{
        restaurantObj.description=@"";
    }
    if([currentElement isEqualToString:@"image_url"]) {
        restaurantObj.image=[NSString stringWithFormat:@"%@",currentElementValue];
        [listOfImages addObject:string];
    }else{
        restaurantObj.image=@"";
    }
    if([currentElement isEqualToString:@"restaurant_type"]) {
        //restaurantObj.Name=[NSString stringWithFormat:@"%@",currentElementValue];
    }else{
        //restaurantObj.name=@"";
    }
    restaurantObj.expires=@"";

    //NSLog (@"ID Array: %@", restaurantObj);
}

-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName {
    //NSLog(@"Current element in End Element Category:- %@",currentElement);
    if([elementName isEqualToString:@"restaurant"]) {
        //NSLog(@"Array: %@", restaurantObj);
        //[listItems addObject:restaurantObj];
    }else{
        currentElementValue=nil;
    }

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog (@"Table Cells: %d",[listOfIds count]);
    return [listOfIds count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"specialsCell";

    DOR_SearchCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil){
        cell = [[DOR_SearchCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    NSLog (@"Name: %@", [listOfNames objectAtIndex:indexPath.row]);

    cell.cellTitle.text = [listOfNames objectAtIndex:indexPath.row];
    cell.cellText.text = [listOfDescriptions objectAtIndex:indexPath.row];
    cell.cellDistance.text = [listOfDistances objectAtIndex:indexPath.row];

    cell.textLabel.text = [listOfNames objectAtIndex:indexPath.row];

    return cell;
}

@end

如果我没有在故事板中输入单元格标识符名称,我的项目将编译并且我得到默认单元格样式(在cell == nil 时设置),这就是我在底部添加cell.textLabel.text 部分的原因用于测试目的。

我真的对 iPhone 编程了解不多。任何帮助将不胜感激。我发现的所有教程似乎都不适合我的项目(在视图控制器中包含表格视图),并且在我尝试实现它们时都不起作用。感谢所有回复。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: uitableview xcode4.2 storyboard identifier


    【解决方案1】:

    问题在于您的 UIImageView 和 UILabel 插座源自您的原型单元。

    原型单元格不是实际的单元格;它作为真实实例的蓝图。当您将 UIImageViews 放置在此蓝图单元中 然后 通过插座将它们绑定到您的类时,Storyboard 不高兴。您基本上是在尝试将抽象的事物与具体的事物联系起来。此时,情节提要失败并出现此不友好的构建错误。

    更多细节在这个帖子:What does the "Couldn't compile connection:" error mean?

    【讨论】:

    • 太棒了,我会改变这个并再试一次,然后让你知道它是如何工作的。谢谢。
    • 实际上,我再次阅读了您的问题,您似乎正在使用自定义单元格。在这种情况下,插座连接应该没有任何问题。无论如何,请查看构建日志以了解实际导致此错误的原因。看这个帖子看看如何打开构建日志:stackoverflow.com/questions/1488931/…
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多