【问题标题】:adding different UILabel to each cell in UITableView为 UITableView 中的每个单元格添加不同的 UILabel
【发布时间】:2011-07-23 09:33:39
【问题描述】:

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

// 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;
}

这是单元格子视图的代码

- (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;
}

【问题讨论】:

    标签: objective-c ios uitableview uilabel


    【解决方案1】:

    initWithFrame:reuseIdentifier: 已被 initWithStyle:reuseIdentifier: 弃用

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

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

    真的不想每次刷新单元格时都重新加载该 plist(哦,有两个!)!在另一个方法中加载一次并将其缓存为 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: 是您的另一个选择,您需要构建完整路径

    【讨论】:

    • hm 只是如你所说改变了,但仍然不知道如何使用 initWithStyle:reuseIdentifier ?我试过 UITableViewCell *cell = [[[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentidier:@"cell"];]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];但它给了我一个警告“没有找到 initWithStyle:reuseIdentifier 方法..”你也能告诉我如何使用 imageWithContentsOfFile:??
    • 所以如果您收到该警告,我猜您使用的是 iOS 5.0 - 您应该同时使用两者,我将编辑代码以显示您的位置
    • imageWithContentsOfFile: 采用 path 就像您在上面加载的 plist 一样,使用相同的 NSBundle 方法来构建/确定您需要发送到该路径的路径.
    • hm 实际上我正在使用 4.2 并且 initWithStyle:reuseIdentifier 现在正在工作,但现在 rankLabel 上没有显示任何内容:( 我在 cell = [self getCellContentView:CellIdentifier]; 之前尝试过,在 cell = [self getCellContentView :CellIdentifier]; 并且没有 cell = [self getCellContentView:CellIdentifier];.. 但它们似乎都不起作用:(
    • 您可能希望在您的 getCell… 方法中同时使用 dequeue/initWithStyle 组合
    【解决方案2】:

    尝试切换这个:

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell = [self getCellContentView:CellIdentifier];
    }
    

    为此:

    UITableViewCell *cell=[self getCellContentView:CellIdentifier];
    

    不是说它会起作用,而是试一试。此外,在您的 getCellContentView 中执行此操作:

    [lblTemp release]
    

    否则你会漏水。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2013-03-07
      • 1970-01-01
      相关资源
      最近更新 更多