我缺少什么使我的自定义UITableView单元工作?

时间:2014-08-31 05:24:10

标签: objective-c xcode 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

1 个答案:

答案 0 :(得分:2)

当您在xib文件中创建一个单元格时,您应该注册nib,而不是类(如果您完全使用代码,则只注册该类)。您应该在viewDidLoad中进行注册。方法dequeueReusableCellWithIdentifier:forIndexPath:保证返回一个单元格,因此永远不会输入if(cell == nil)子句,这就是为什么在那里注册没有效果。