1

我创建了一个名为“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,我将视图引用到我的类“SinglePrevViewController”,它扩展了“UITableViewController”。我为 tableView 实现了经典方法(numberOfRowsInSection:等......)。

在“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 包含我的 CustomPrevCells 并正确显示内容。但是......当我想从这些单元格中推送一个新的 viewController 时,它不起作用。我已将我的 customCell(从情节提要中 TableView 中的单元格)连接到新的 viewcontroller(名为 DetailPrevViewcontroller),但它什么也没做。我的 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/ 为要推送的视图控制器创建一个新的 nib(不在情节提要中)

2/ 将它设置为您的 ViewController 的类(在我的情况下为 DetailPrevViewController)和 FilesOwner 的视图到 NIB 视图

3/ 使用 navigationController 从方法“didSelectRowAtPath”推送视图。

我不喜欢这种方式进行,但它是目前唯一可行的方式......

4

2 回答 2

1

确保您已连接情节提要中的单元格,而不是整个视图控制器。这很容易发生。一种方法是首先选择单元格,然后进行拖动。

没有必要实施didSelectRowAtIndexPath。这将使整个故事板变得毫无用处......

于 2012-07-13T09:26:38.387 回答
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"];
    }
}
于 2012-07-13T09:24:04.073 回答