【发布时间】:2016-07-19 04:08:42
【问题描述】:
我在处理UITableView 时遇到了一个有趣的问题。如果有人能指出我在哪里犯了错误,我将不胜感激。
所以,情况是:我有一个NSDictionaries 的NSArray,我试图让UITableView 中的每一行都显示相应的NSDictionary。下面是“cellForRowAtIndexPath”方法的代码片段:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure custom cell
static NSString *cellReuseId = @"cell";
SearchTableViewCell *searchCell = [tableView dequeueReusableCellWithIdentifier:cellReuseId forIndexPath:indexPath];
if (self.realData.count > 0) {
self.tempDict = self.realData[indexPath.row];
[searchCell initLabels:[self.tempDict valueForKey:@"psnname"] withDept:[self.tempDict valueForKey:@"deptname"] withPostion:[self.tempDict valueForKey:@"position"] withTel:[self.tempDict valueForKey:@"tel"] withFax:[self.tempDict valueForKey:@"fax"]];
} else {
NSLog(@"no data");
}
return searchCell;
}
我创建了一个自定义 UITableViewCell,并尝试通过调用“initLabels...”方法在 custom UITableViewCell 中初始化五个 UILabels。下面是自定义UITableViewCellclass中的"initLabels..."方法:
-(void) initLabels:(NSString *)psn withDept:(NSString *)dept withPostion:(NSString *)pos withTel:(NSString *)tell withFax:(NSString *)faxx{
int tempWidth = self.frame.size.width/5;
self.psnname = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + 10, self.frame.origin.y, tempWidth * 0.8, self.frame.size.height)];
self.deptname = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + self.psnname.frame.size.width + 5, self.frame.origin.y , tempWidth * 1.1, self.frame.size.height)];
self.position = [[UILabel alloc] initWithFrame:CGRectMake(self.deptname.frame.origin.x + self.deptname.frame.size.width + 5, self.frame.origin.y , tempWidth * 1.1, self.frame.size.height)];
self.tel = [[UILabel alloc] initWithFrame:CGRectMake(self.position.frame.origin.x + self.position.frame.size.width + 5, self.frame.origin.y , tempWidth * 0.7, self.frame.size.height)];
self.fax = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width - tempWidth * 1.1, self.frame.origin.y , tempWidth, self.frame.size.height)];
self.psnname.text = psn;
self.deptname.text = dept;
self.position.text = pos;
self.tel.text = tell;
self.fax.text = faxx;
[self.contentView addSubview:self.psnname];
[self.contentView addSubview:self.deptname];
[self.contentView addSubview:self.position];
[self.contentView addSubview:self.tel];
[self.contentView addSubview:self.fax];
}
问题是:在使用NSString's 分配UILabel's 之前初始化那些UILabels 时,布局格式正确,但显示数据不正确;而如果在设置 UILabel 的框架之前使用 NSString 进行分配,则数据正确显示,但布局格式不正确。
导致这种情况发生的潜在原因是什么??
【问题讨论】:
-
细胞被重复使用。想想当您在同一个单元格实例上一遍又一遍地调用
initLabels时会发生什么。 -
顺便说一句 - 用
init前缀命名非初始化方法是个坏主意。 -
你能贴出你的问题的截图吗?
标签: ios objective-c uitableview uilabel