2014-08-31 55 views
0

我創建按以下方式定製的細胞我錯過了什麼使我的自定義UITableView單元格工作?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    FacilityResult* currentResult = [_facilityResults objectAtIndex:indexPath.row]; 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"facilityResultDesign" forIndexPath:indexPath]; 
    } 
    return cell; 
} 

當創建FacilityResult對象,它是自定義小區我更新標籤如下類:

@implementation FacilityResult 


- (id)initWithModel:(NSString *)name city:(NSString*)city zipcode:(NSString*)zipcode facility_id:(NSString*)facility_id{ 
    _name = name; 
    _zipcode = zipcode; 
    _city = city; 
    _facility_id = facility_id; 
    return self; 
} 

- (id)initwithModel:(NSMutableDictionary*)propertyDictionary{ 
    if (![[propertyDictionary allKeys] containsObject:@"name"]) 
     NSLog(@"Error: Cell Object does not have a name"); 
    else{ 
     _name = propertyDictionary[@"name"]; 
     [propertyDictionary removeObjectForKey:@"name"]; 
     [_nameField setText:_name]; 
    } 
    if (![[propertyDictionary allKeys] containsObject:@"city"]) 
     NSLog(@"Error: Cell Object does not have a city"); 
    else{ 
     _city= propertyDictionary[@"city"]; 
     [propertyDictionary removeObjectForKey:@"city"]; 
     [_cityField setText:_city]; 
    } 
return self; 
} 


@end 

- 更新viewDidLoad中

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    [[login getSearchFacility] setDelegate:self]; 
    [_resultTable registerNib:[UINib nibWithNibName:@"FacilityRow" bundle:nil] forCellReuseIdentifier:@"facilityResultDesign"]; 
    if(_facilityResults==nil) 
     _facilityResults = [[NSMutableArray alloc] init]; 
} 

然而,標籤不更新「有些公司的」細胞是我對Xcode的標籤輸入的默認名稱。它應該更新爲name1。 enter image description here

+0

請按xib編輯器上的按鈕「顯示文檔大綱」顯示所有樹形視圖 – stosha 2014-08-31 06:27:30

回答

2

當你在一個xib文件中創建一個單元格時,你應該註冊nib而不是類(如果你完全用代碼完成單元的話,只註冊這個類)。你應該在viewDidLoad中註冊。方法dequeueReusableCellWithIdentifier:forIndexPath:保證返回一個單元格,所以你的if(cell == nil)子句永遠不會被輸入,這就是爲什麼註冊沒有效果。

+0

我同意,它從不進入if cell == nil。那麼我如何註冊xib呢?謝謝 – 2014-08-31 05:39:06

+0

@Alon_T,就像我在回答中所說的那樣。在viewDidLoad中做。 – rdelmar 2014-08-31 05:40:01

+0

我使用什麼功能?我真的不確定 – 2014-08-31 05:42:19