向 UITableView 中的每个单元格添加不同的 UILabel [英] adding different UILabel to each cell in UITableView

查看:28
本文介绍了向 UITableView 中的每个单元格添加不同的 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向 UITableView 中的每个单元格添加不同的文本.但是,当我这样做时,除了第一个单元格之外,每个单元格中都会显示 test..So 如果有一个数组,其中包含 1 到 9 之间的 9 个数字,则在第二个单元格中显示 1,在第三个单元格中分别显示 2.代码的第一个单元格中没有显示任何内容

I'm adding different text to each cell in UITableView. However when I do that, test is displayed in every cell except for the first one..So if theres an array with 9 numbers between 1 to 9, 1 is displayed in the second cell, 2 is displayed in the third cell and respectively. There's nothing shown in the first cell fromHeres the codes

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell = [self getCellContentView:CellIdentifier];
}
if (indexPath.row == 0) {
    NSLog(@"hi");
}
//add another textfield
NSString *path = [[NSBundle mainBundle] pathForResource:@"Rankvalue" ofType:@"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *rankValue = [rank objectAtIndex:indexPath.row];

UILabel *rankLabel = (UILabel *)[cell viewWithTag:1];
rankLabel.text = rankValue;
[rankLabel setFont:[UIFont fontWithName:@"austin power" size:[@"40" intValue]]];

CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];

// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:path2];
cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];

//transparent cell
cell.backgroundColor = [UIColor clearColor];

[rankLabel release];
[rankValue release];

return cell;
}

这是cell子视图的代码

And this is the code for the subview of the cell

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 320, 65);
CGRect Label1Frame = CGRectMake(17,5,250,18);  

UILabel *lblTemp;


UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];  
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:@"Arial-Bold" size:15]];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];    

return cell;
}

推荐答案

initWithFrame:reuseIdentifier: 已弃用 initWithStyle:reuseIdentifier:

您应该创建一种标准样式.

You should create one of the standard styles.

在单元格创建时设置您想要的值(字体等),而不是在刷新时设置.文本字段的框架是相同的.和背景颜色.

Set the values you want (font, etc.) on cell creation, not on refresh. Frame of text field is the same. And background color.

真的不想在每次刷新单元格时重新加载该 plist(哦,有两个!)!在另一个方法中加载一次并将其缓存为 ivar.

You really don't want to reload that plist (oh, there's two of them!) every time you refresh a cell! Load it once in another method and cache it as an ivar.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    // create and set up all the layout attributes of the cell
    // it should be auto released
    // which should include a call like the following, or loading a cell from a nib
    // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
    //                               reuseIdentifier:CellIdentifier];
    // [cell autorelease];
  }

  // the only thing that should happen here is setting the data values
  // into the cell!!!!!!  All these values should be loaded once and
  // kept as "model state" by the controller.  Do not create an image
  // each time through, do not load an entire plist to access one item
  // each time through

  rankLabel.text = rankValue;
  cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
  cell.imageView.image = [self imageAtRow:indexPath.row];


}

如果每个单元格显示的图像都不同,则使用 imageNamed: 是不合适的.如果有 2 或 3 张图像指示每个将被大量使用的单元格类型,imageNamed: 可能更可取.imageWithContentsOfFile: 是您的另一个选择,您需要构建完整路径

If the image your displaying is different for each cell, using imageNamed: is not appropriate. If there's 2 or 3 images that indicate type of cell that will each be used a lot, imageNamed: is likely preferable. imageWithContentsOfFile: is your other option, you'll need to build the full path

这篇关于向 UITableView 中的每个单元格添加不同的 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆