我已经创建了一个名为"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中显示这个单元格(当然!)。因此,在我的故事板中,我用一个UITableView原型创建了一个新的CustomCell,并将视图引用到我的类"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中的单元格)连接到新的视图控制器(名为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 : Ok --我还没有这个问题的解决方案,但是我通过这样做来消除它:
1/为您想要推送的视图控制器创建一个新的nib (而不是在故事板中)
2/将ViewController类(在我的例子中是DetailPreviViewController)和FilesOwner视图设置为NIB视图
3/使用didSelectRowAtPath从方法“navigationController”中推送视图。
我不喜欢这样做,但现在,唯一可行的方法是.
发布于 2012-07-13 09:26:38
确保您已经连接了情节提要中的单元格,而不是整个视图控制器。这是很容易发生的。一种方法是首先选择,选择单元格,然后执行拖动。
不需要实现didSelectRowAtIndexPath。这会让整个故事板都没用..。
发布于 2012-07-13 09:24:04
编辑的回答:
试着实现这个
-(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"];
}
}https://stackoverflow.com/questions/11467510
复制相似问题