Ios UITableViewCell将数据分配给UILabel后显示不正确的数据

Ios UITableViewCell将数据分配给UILabel后显示不正确的数据,ios,objective-c,uitableview,uilabel,Ios,Objective C,Uitableview,Uilabel,我在处理UITableView时遇到了一个有趣的问题。如果有人能指出我犯了什么错误,我将不胜感激 因此,场景是:我有一个NSDictionary的NSArray,我试图让UITableView中的每一行都显示相应的NSDictionary。下面是“cellforrowatinexpath”方法的代码片段: 我制作了一个自定义的UITableViewCell,并试图通过调用“initLabels…”方法初始化customUITableViewCell中的五个UILabels。以下是自定义UITab

我在处理
UITableView
时遇到了一个有趣的问题。如果有人能指出我犯了什么错误,我将不胜感激

因此,场景是:我有一个
NSDictionary
NSArray
,我试图让
UITableView
中的每一行都显示相应的
NSDictionary
。下面是“
cellforrowatinexpath
”方法的代码片段:

我制作了一个自定义的
UITableViewCell
,并试图通过调用“initLabels…”方法初始化custom
UITableViewCell
中的五个
UILabels
。以下是自定义
UITableViewCell
类中的“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
分配
UILabel
之前初始化这些
UILabel
时,布局格式正确,但显示数据不正确;如果在设置UILabel的帧之前使用NSString赋值,则数据将正确显示,但布局不符合格式。


导致这种情况发生的潜在原因是什么???

您应该将构建自定义表视图单元格的视图层次结构的代码与填充数据的代码分开

在自定义表视图单元格的
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;
}

另一种方法是,只需在
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;
}

细胞被重新利用。想想当你在同一个计算单元实例上反复调用
initLabels
时会发生什么。顺便说一句——用前缀
init
命名一个非初始化方法是个坏主意。你能发布你的问题的屏幕截图吗。
-(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;
}