2013-01-11 49 views
1

我有一个tableview应该填充一个NSMutableArray。这工作得很好,没有定制原型UITableViewCell。我有一个单元格的文件,名为SimpleTableCell。我的故事板中的原型单元格已将此类指定给它。 SimpleTableCell.h的内容:标签没有在UITableView中正确实现,为什么?

#import <UIKit/UIKit.h> 

@interface SimpleTableCell : UITableViewCell 
@property (nonatomic, weak) IBOutlet UILabel *nameLabel; 
@property (nonatomic, weak) IBOutlet UILabel *moreInfoLabel; 
@end 

这被导入到viewController在那里我有tableView,就像这样: CargoViewController.h

#import <UIKit/UIKit.h> 
#import "StartupViewController.h" 
#import "boatInfoViewController.h" 
#import "SimpleTableCell.h" 

@interface CargoViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UIPickerViewDataSource> 

@property(strong,nonatomic) IBOutlet UILabel *testLabel; //label I use to test stuff instead of LOG 
@property(strong,nonatomic) IBOutlet UIButton *findOwner; //button to summon pickerWheel 
@property(strong,nonatomic) IBOutlet UIButton *findShip; //Button to confirm selection in picker and unhide tableView 
@property(strong,nonatomic) NSArray *boatOwners; //Array for boat owners. 
@property(strong,nonatomic) NSMutableArray *boatsForOwner; //array for boats, added by owner 
@property(strong,nonatomic) IBOutlet UITableView *boatsTableView; //Table view 
-(IBAction)findOwnerButtonPressed:(id)sender; 
-(IBAction)findShipButtonPressed:(id)sender; 
@property(strong, nonatomic) IBOutlet UIPickerView *shipOwners; //ship owner picker wheel 
@property(strong,nonatomic)IBOutlet UILabel *infoLabel; 
/*@property(strong,nonatomic)IBOutlet UILabel *nameLabel; //These was tested, but didn't work. Illegal assignment error. 
@property(strong,nonatomic)IBOutlet UILabel *moreInfoLabel; 
@property(strong,nonatomic)IBOutlet UITableViewCell *SimpleTableCell;*/ 

@end 

此,再次向控制 CargoViewController.m(仅添加相关部件)

#import "CargoViewController.h" 
#import "SimpleTableCell.h" 
@interface CargoViewController() 


@end 

@implementation CargoViewController 
@synthesize testLabel, findOwner, findShip,shipOwners, boatsTableView, infoLabel; 
@synthesize boatOwners, boatsForOwner; 

//When I try to synthesize name and infolabel in cell here, it does not work 

//...more code... 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [boatsForOwner count]; 
} 

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

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

cell.nameLabel.text = [boatsForOwner objectAtIndex:indexPath.row]; 
return cell; 
} 

我的问题是: cell.nameLabel.text不上对象UITableViewCell 其中发现,根据enter image description here我的故事板是错误的

如需进一步资料,该表的工作之前,我实现了原型细胞(?)。在我使用默认值之前。 错误触发:

#pragma mark - 
#pragma mark PickerView DataSource 

- (NSInteger)numberOfComponentsInPickerView: 
(UIPickerView *)pickerView 
{ 
return 1; 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component 
{ 
return [boatOwners count]; 
} 
- (NSString *)pickerView:(UIPickerView *)pickerView 
     titleForRow:(NSInteger)row 
     forComponent:(NSInteger)component 
{ 
return [boatOwners objectAtIndex:row]; 
} 


#pragma mark - 
#pragma mark PickerView Delegate 
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
    inComponent:(NSInteger)component 
{ 
NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]]; 
//if(boatsForOwner) {[boatsForOwner release]; boatsForOwner = nil;} 
boatsForOwner = [[NSMutableArray alloc] init]; 
if ([boatsFromOwner isEqualToString:@"Owner1"]) { 
    [self.boatsForOwner addObject:@"Titanic1"]; 
    [self.boatsForOwner addObject:@"Titanic2"]; 
    [self.boatsForOwner addObject:@"Titanic3"]; 
    [self.boatsForOwner addObject:@"Titanic4"]; 
    [self.boatsForOwner addObject:@"Titanic5"]; 
    [self.boatsForOwner addObject:@"Titanic6"]; 
    [self.boatsForOwner addObject:@"Titanic7"]; 

} 
NSLog(@"%@",boatsFromOwner); 

[boatsTableView reloadData]; 

} 

回答

1

改变这一行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

此:

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

顺便说一句,用故事板时tableView:dequeueReusableCellWithIdentifier:永远不会返回nil

+0

如果它永远不会返回'nil'how我应该代替落实细胞? –

+0

+当我按照你所说的,这个 LNG [44650:c07] ***声明失败 - [UITableView _configureCellForDisplay:forIndexPath:],/ SourceCache /UIKit_Sim/UIKit-2372/UITableView.m:5471 LNG [ 44650:c07] ***终止应用程序,由于未捕获的异常'NSInternalInconsistencyException',原因:'UITableView dataSource必须从tableView:cellForRowAtIndexPath:'发生一个单元格。固定? –

+0

你不必担心实例化,iOS会为你做这个 – tkanzakic

0
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

尝试改变

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
相关问题