为 UITableView 中的每个单元格添加不同的 UILabel
我正在向 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;
}
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
initWithFrame:reuseIdentifier:
已被弃用,initWithStyle:reuseIdentifier:
您应该创建一种标准样式。
在创建单元格时设置所需的值(字体等),而不是在刷新时设置。文本字段的框架是相同的。和背景颜色。
您真的不想在每次刷新单元格时重新加载该plist(哦,有两个!)!在另一个方法中加载一次并将其缓存为 ivar。
如果每个单元格显示的图像不同,则使用
imageNamed:
是不合适的。如果有 2 或 3 个图像指示将被频繁使用的单元格类型,则imageNamed:
可能更可取。imageWithContentsOfFile:
是您的另一个选择,您需要构建完整路径initWithFrame:reuseIdentifier:
has been deprecated forinitWithStyle: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.
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.
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尝试切换这个:
为此:
并不是说它会起作用,而是试一试。另外,请在 getCellContentView 内执行此操作:
否则您将发生泄漏。
Try switching this:
for this:
Not saying it will work, but just give it a shot. Also, do this inside your getCellContentView:
Or you will have a leak.