我创建了一个名为“CustomPreviCell”的自定义单元格(它是NIB的名称和此NIB中单元格的标识符)。我的customCell包含6个标签和一个imageView。与此单元格对应的.m文件如下:
@interface CustomPreviCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *weatherLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *monthLabel;
@end
并实施:
#import "CustomPreviCell.h"
@implementation CustomPreviCell
@synthesize iconImageView;
@synthesize weatherLabel;
@synthesize detailOneLabel;
@synthesize detailTwoLabel;
@synthesize dayNumberLabel;
@synthesize dayTextLabel;
@synthesize monthLabel;
- (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];
}
@end
现在,我想在tableView中显示这个单元格(当然!)。所以在我的故事板中,我创建了一个带有一个CustomCell原型的新UITableView,我将视图提到我的类“SinglePreviViewController”,它扩展了“UITableViewController”。我为tableView实现了经典方法(numberOfRowsInSection:etc ...)。
在方法“cellForRowAtIndexPath”中,我想创建,初始化和显示我的自定义单元格,所以我写了:
NSString *simpleTableIdentifier = @"CustomCellPrevi";
CustomPreviCell *cell = (CustomPreviCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellPrevi" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[CustomPreviCell class]]){
cell = (CustomPreviCell *)currentObject;
NSString * iconName = [self loadIcon:indexPath.row];
[cell.iconImageView setImage:[UIImage imageNamed:iconName]];
cell.detailOneLabel.text = [[NSString alloc] initWithFormat:@"%@°C - %@mm",[descriptor.temperature objectAtIndex:indexPath.row], [descriptor.precipitations objectAtIndex:indexPath.row]];
cell.detailTwoLabel.text = [[NSString alloc] initWithFormat:@"%@ (%@) km/h - %@hPa",[descriptor.vent objectAtIndex:indexPath.row],[descriptor.rafales objectAtIndex:indexPath.row], [descriptor.pression objectAtIndex:indexPath.row]];
cell.weatherLabel.text = [descriptor.temps objectAtIndex:indexPath.row];
NSDictionary * date = [self getDateComponentsFromXMLDate:[descriptor.date objectAtIndex:indexPath.row]];
if([[date valueForKey:@"dayNumber"] intValue] %2 == 0)
[cell.contentView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.0]];
else
[cell.contentView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.15]];
cell.dayTextLabel.text = [date valueForKey:@"dayText"];
cell.dayNumberLabel.text = [[NSString alloc] initWithFormat:@"%@ %@",[date valueForKey:@"dayNumber"],[date valueForKey:@"month"]];
cell.monthLabel.text = [date valueForKey:@"hour"];
}
}
return cell;
让我们启动我的应用:它工作正常!我的tableView包含我的CustomPreviCells并正确显示内容。但是......当我想从这些单元格中推出一个新的viewController时,它无法工作。我已经将我的customCell(从故事板中的TableView中的单元格)连接到新的viewcontroller(名为DetailPreviViewcontroller),但它什么也没做。我的customCell刚刚被选中。
请帮助:)
编辑1:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"Hey !");
if([segue.identifier isEqualToString:@"propertiesSegue"])
{
NSLog(@"Hello");
SingleVillePropertiesViewController * destinationController = [segue destinationViewController];
[destinationController setVille:ville];
}
else if([segue.identifier isEqualToString:@"detailPreviSegue"])
{
DetailPreviViewController * destController = [segue destinationViewController];
[[self navigationController] pushViewController:destController animated:YES];
//TODO : set previDescriptor to my destController but it's not important
// for this problem.
}
}
编辑2: 好吧,我还没有解决这个问题,但我通过这样做摆脱它:
1 /为要推送的视图控制器创建新的笔尖(不在故事板中)
2 /将它设置为ViewController的类(在我的案例中为DetailPreviViewController),将FilesOwner视图设置为NIB视图
3 /使用navigationController从方法“didSelectRowAtPath”推送视图。
我不喜欢这种方式继续进行,但它现在是唯一可行的......
答案 0 :(得分:1)
确保已连接故事板中的单元格,而不是整个视图控制器。这很容易发生。一种方法是首先选择单元格,然后进行拖动。
无需实施didSelectRowAtIndexPath
。那会让整个故事板变得毫无用处......
答案 1 :(得分:0)
编辑了segues的答案:
尝试实施此
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"prepareForSegue: %@", segue.identifier);
if ([segue.identifier isEqualToString:@"A"]) {
[segue.destinationViewController setValue1:@"something"];
// Maybe you have to alloc init your view controller also
} else if ([segue.identifier isEqualToString:@"B"]) {
[segue.destinationViewController setValue1:@"something else"];
}
}