2012-07-13 91 views
1

我已經創建了一個名爲「CustomPreviCell」(它是NIb的名稱和此NIB中單元的標識符)的自定義單元。我的customCell包含6個標籤和一個imageView。對應於該小區的.m文件是以下幾點:從au CustomCell推送一個ViewController

@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,並且將視圖引用到擴展了「UITableViewController」的我的類「SinglePreviViewController」中。我實現了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(從Storyboard中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的筆尖查看

3查看/從推方法「didSelectRowAtPath」與navigationController的視圖。

我不喜歡這樣的方式來進行,但它是現在,這唯一可行的......

+0

你實現了'tableView:didSelectRowAtIndexPath:'? – 2012-07-13 09:17:02

+0

如果他正在使用storyboard segues,則無需執行'didSelectRowAtIndexPath' ... – Mundi 2012-07-13 09:24:07

+0

我不需要實現didSelectRowAtPath。我沒有參加過其他一些課程,它的工作完美。對於這個TableView,我只想通過「prepareForSegue」將描述符設置爲我的DetailPreviViewController,但是當我點擊一個customCell時,不調用此方法。 – QLag 2012-07-13 09:32:22

回答

0

的塞格斯編輯答案:

嘗試實施這一

-(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"]; 
    } 
} 
+0

正如我在上面的帖子中所說的,我不需要實現這個功能。但我試圖實現(只是爲了嘗試),並從這裏推我的DetailPreviViewController。它推動我一個空視圖! (另一個神祕的問題......) – QLag 2012-07-13 09:34:23

+0

好的嘗試在「prepareForSegue:」方法中創建新的viewController – 2012-07-13 10:57:05

+0

當我選擇一個單元格時,我從不傳入此方法。這是一個大問題。你可以在我的帖子的EDIT1中看到prepareForSegue的代碼。 當我點擊屬性按鈕(在我的頂部欄),它的工作原理和第一個條件(標識= propertiesSegue)是好的。 當我選擇一行,prepapreForSegue不叫...... – QLag 2012-07-13 12:14:41

1

確保您連接了故事板中的單元格,而不是整個視圖控制器。這很容易發生。一種方法是先將選中這個單元格然後再拖動。

沒有必要實施didSelectRowAtIndexPath。這將使整個故事板segues無用...

+0

當我選擇我的單元格和下一個視圖之間的鏈接時,我的單元格顯示爲藍色=>這裏不是問題:/但我同意你的看法:不需要實現didSelectRowAtIndexPath ... – QLag 2012-07-13 09:37:03

相關問題