在处理UITableView
时遇到了一个有趣的问题。如果有人能指出我犯了哪些错误,我将非常感激。
所以,情况是:我有NSArray
NSDictionaries
,我正在尝试让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 ...”方法在自定义 UILabels
中初始化五个UITableViewCell
。以下是自定义UITableViewCell
class
中的“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];
}
问题是:在使用UILabels
分配UILabel's
之前初始化NSString's
时,布局格式正确但显示数据不正确;如果在设置UILabel的帧之前使用NSString进行分配,则数据正确显示但布局超出格式。
导致这种情况发生的潜在原因是什么?
答案 0 :(得分:2)
您应该将构建自定义表格视图单元格的视图层次结构的代码与使用数据填充它的代码分开。
在自定义表格视图单元格init
(或其初始值设定项)中添加以下内容:
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.contentView addSubview:self.psnname];
[self.contentView addSubview:self.deptname];
[self.contentView addSubview:self.position];
[self.contentView addSubview:self.tel];
[self.contentView addSubview:self.fax];
然后在表视图单元子类中创建一个方法 - 类似于以下内容 - 并从您的cellForRowAtIndexPath中调用该方法
- (void)populateWithPSN:(NSString *)psn dept:(NSString *)dept postion:(NSString *)pos tel:(NSString *)tell fax:(NSString *)fax
{
self.psnname.text = psn;
self.deptname.text = dept;
self.position.text = pos;
self.tel.text = tell;
self.fax.text = faxx;
}
答案 1 :(得分:0)
另一种方法是,只需初始化init
方法中的标签并在自定义单元格的layoutSubviews
方法中设置标签的帧,在显示单元格之前多次调用此方法,这是设置标签框架的最佳位置,例如
在您的自定义单元格.m
文件
-(void)layoutSubviews
{
[super layoutSubviews];
self.psnname.frame = CGRectMake(self.frame.origin.x + 10, self.frame.origin.y, tempWidth * 0.8, self.frame.size.height);
self.deptname.frame = CGRectMake(self.frame.origin.x + self.psnname.frame.size.width + 5, self.frame.origin.y , tempWidth * 1.1, self.frame.size.height);
self.position.frame = 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.frame = 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.frame = CGRectMake(self.frame.size.width - tempWidth * 1.1, self.frame.origin.y , tempWidth, self.frame.size.height);
}
并且为了设置数据你可以使用你的方法,但为了更好的可读性,你可以像
一样修改它-(void)setLabels:(NSString *)psn withDept:(NSString *)dept withPostion:(NSString *)pos withTel:(NSString *)tell withFax:(NSString *)fax
和细胞创建方法,
-(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 setLabels:[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;
}