我已經創建了一個名爲「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的視圖。
我不喜歡這樣的方式來進行,但它是現在,這唯一可行的......
你實現了'tableView:didSelectRowAtIndexPath:'? – 2012-07-13 09:17:02
如果他正在使用storyboard segues,則無需執行'didSelectRowAtIndexPath' ... – Mundi 2012-07-13 09:24:07
我不需要實現didSelectRowAtPath。我沒有參加過其他一些課程,它的工作完美。對於這個TableView,我只想通過「prepareForSegue」將描述符設置爲我的DetailPreviViewController,但是當我點擊一個customCell時,不調用此方法。 – QLag 2012-07-13 09:32:22