1

我有一个 tableView 应该填充一个NSMutableArray. 这在没有自定义原型的情况下工作得很好UITableViewCell。我有一个自己的单元格文件,名为SimpleTableCell. 我的故事板中的原型单元有这个类分配给它。SimpleTableCell.h的内容 :

#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 哪个,根据在此处输入图像描述我的故事板是错误的(?)

有关更多信息,该表在我实现原型单元之前有效。在我只使用默认值之前。错误触发:

#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];

}
4

2 回答 2

1

改变这一行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

为了这:

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

顺便说一句,使用情节提要时tableView:dequeueReusableCellWithIdentifier:将永远不会返回nil

于 2013-01-11T11:29:43.520 回答
0
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

尝试更改为

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
于 2013-01-11T11:33:13.627 回答