ios)添加不同的UILabel到UITableView中的每个单元格的objective-c问题

我将不同的文本添加到UITableView中的每个单元格。 但是当我这样做时,testing显示在除了第一个以外的每个单元格中。因此,如果在9到9之间的9个数字的数组显示在第二个单元格中,则分别在第三个单元格中显示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; } 

谢谢:)

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

您应该创build一个标准样式。

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

每次刷新单元格时,你真的不想重新加载plist(哦,其中有两个)! 在另一个方法中加载一次,并将其caching为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个图像指示单元格的types,那么imageNamed:可能更可取。 imageWithContentsOfFile:是你的另一种select,你需要build立完整的path

哼,试试这个:

 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] 

否则你会有泄漏。