【问题标题】:Push a ViewController from au CustomCell从 au CustomCell 推送 ViewController
【发布时间】:2012-07-13 09:14:24
【问题描述】:

我创建了一个名为“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”推送视图。

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

【问题讨论】:

  • 你实现tableView:didSelectRowAtIndexPath:了吗?
  • 如果他使用故事板转场,则无需实现didSelectRowAtIndexPath...
  • 我不需要实现 didSelectRowAtPath。我没有参加其他课程,而且效果很好。对于这个 TableView,我只想通过“prepareForSegue”为我的 DetailPrevViewController 设置一个描述符,但是当我点击一个 customCell 时,这个方法不会被调用。

标签: iphone pushviewcontroller custom-cell


【解决方案1】:

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

无需实现didSelectRowAtIndexPath。这将使整个故事板继续无用...

【讨论】:

  • 当我选择我的单元格和下一个要推送的视图之间的链接时,我的单元格是蓝色突出显示 => 这不是问题:/ 但我同意你的观点:无需实现 didSelectRowAtIndexPath.. .
【解决方案2】:

已编辑的 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"];
    }
}

【讨论】:

  • 正如我在上面的帖子中所说,我不需要实现这个功能。但是我尝试实现(只是尝试)并从这里推送我的 DetailPrevViewController。它给我一个空的视图! (另一个神秘的问题......)
  • 好的尝试在“prepareForSegue:”方法中创建新的viewController
  • 当我选择一个单元格时,我从不传入这个方法。这是一个大问题。您可以在我的帖子的 EDIT1 中看到 prepareForSegue: 的代码。当我单击属性按钮(在我的顶部栏中)时,它可以工作并且第一个条件(标识符 = propertiesSegue)是可以的。当我选择一行时,不会调用 prepapreForSegue...
  • 选择您的表格视图,cmd+click,将箭头转到您的视图控制器并选择委托
  • 是的委托和数据源已经链接到我的视图控制器...(如果不是这样,我无法在我的单元格中加载数据,但我可以)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 2014-11-12
  • 2018-10-11
  • 2013-01-31
  • 2013-12-18
相关资源
最近更新 更多