我正在使用自定义单元格的tableview。
以下是我的cellForRowAtIndexPath方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// AssigneeCell *cell = (AssigneeCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
static NSString *cellIdentifier = @"Cell";
AssigneeCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"AssigneewiseView" bundle:nil] forCellReuseIdentifier:@"Cell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSLog(@"call");
}
cell.unitNameVal = unitNameResult[indexPath.row];
cell.domainNameVal = domainNameResult[indexPath.row];
cell.compliedVal = compliedResult[indexPath.row];
cell.delayedVal = delayedResult[indexPath.row];
cell.inProgressVal = inProgressResult[indexPath.row];
cell.notCompliedVal = notCompliedResult[indexPath.row];
cell.totalVal = totalValResult[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [compliedResult count];
}
调用numberOfRowsInSection,因此我的tableview包含多个视图,具体取决于compliedResult的数量。
但问题是,我的自定义单元格中存在的uilabels的值未被分配,因此不可见。为什么会这样?
答案 0 :(得分:2)
由于unitNameVal
,domainNameVal
等是标签,因此请确保您要设置其文字属性。你现在正在做的是将标签本身设置为不同的对象。
假设你的数组是字符串数组,这应该可以解决这个问题:
cell.unitNameVal.text = unitNameResult[indexPath.row];
cell.domainNameVal.text = domainNameResult[indexPath.row];
...
答案 1 :(得分:2)
首先建议,创建你的模型:
@interface Result: NSObject
@property (nonatomic) NSString* unitName;
@property (nonatomic) NSString* domainName;
@property (nonatomic) NSString* complied;
@property (nonatomic) NSString* delayed;
@property (nonatomic) NSString* inProgress;
@property (nonatomic) NSString* notComplied;
@property (nonatomic) NSString* totalVal;
@end
这样,你的viewController将有一个Result对象数组。
在自定义单元格中,定义:
@implementation AssigneeCell
+ (NSString*)cellIdentifier {
return @"Cell";
}
- (void)setupWithResult:(Result*)result {
self.unitNameVal.text = result.unitName;
self.domainNameVal.text = result.domainName;
self.compliedVal.text = result.complied;
self.delayedVal.text = result.delayed;
self.inProgressVal.text = result.inProgress;
self.notCompliedVal.text = result.notComplied;
self.totalVal.text = result.totalVal;
}
@end
最后在你的视图控制器中:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView registerNib:[UINib nibWithNibName:@"AssigneewiseView" bundle:nil] forCellReuseIdentifier:[AssigneeCell cellIdentifier]];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString* cellIdentifier = [AssigneeCell cellIdentifier];
AssigneeCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell setupWithResult: results[indexPath.row]];
return cell;
}
这可确保干净代码和代码重用。