【发布时间】:2023-03-08 07:57:01
【问题描述】:
我正在尝试将 plist 中从 1 到 9 的数字添加到 UITableView 中的每个单元格。然而,第一个视图没有显示任何内容,1 显示在第二个单元格中,2 显示在第三个单元格中,依此类推。我通过在if(cell==nil) 中添加NSLog(@"test"); 进行了一些测试,但是在有9 个单元格时只打印了7 个“测试”。这是否意味着if(cell==nil) 中的代码不会针对第一个和最后一个单元格执行?
有人关心解决这个问题吗? :(
// 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)
{
NSLog(@"hi");
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [self getCellContentView:CellIdentifier];
//add another textfield
NSString *rankpath = [[NSBundle mainBundle] pathForResource:@"Rankvalue" ofType:@"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:rankpath];
rankValue = [rank objectAtIndex:indexPath.row];
rankLabel = (UILabel *)[cell viewWithTag:1];
// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:@"ALBA" size:[@"25" intValue]]];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:filepath];
//transparent cell
cell.backgroundColor = [UIColor clearColor];
}
//label
rankLabel.text = rankValue;
//[rankLabel setFont:[UIFont fontWithName:@"austin power" size:[@"40" intValue]]];
//rankLabel.textColor = [UIColor redColor];
CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];
cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];
//cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];
return cell;
}
这是我为子视图添加的另一种方法
//new method for different text in each 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
//UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:@"austin power" size:40]];
lblTemp.textColor = [UIColor redColor];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];
return cell;
}
谢谢 哦,顺便说一句,我正在使用它的模拟器 4.2。
【问题讨论】:
-
听起来像是数组中从 0 开始的索引有问题...
-
我也有同样的问题,而且可以肯定的是,我的数组在索引 0 处没有零或零。
标签: objective-c ios uitableview uilabel