UITableView 自定义单元格出现问题
我正在尝试将自定义标签添加到 UITableView 中的单元格中。当我尝试执行此操作时,显示混乱,我无法弄清楚到底发生了什么。请找到下面的图片,我将发布我写的方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell.textLabel.font=[UIFont systemFontOfSize:16];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
NSArray *contactSection=[contactNames objectForKey:contact];
NSMutableArray *sugar=[db sugarId];
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
NSArray *splitText = [cellValue componentsSeparatedByString:@":"];
NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
//NSString *result=[contactText stringByAppendingString:[sugar objectAtIndex:[indexPath row]]];
firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];
firstLabel.tag =40; //This should be a constant probably
firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
firstLabel.text = contactText;
[firstLabel sizeToFit];
[cell.contentView addSubview:firstLabel];
sugarLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
sugarLabel.tag =40; //This should be a constant probably
//sugarLabel.font = [UIFont boldSystemFontOfSize:16];
sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
[sugarLabel setHidden:YES];
[cell.contentView addSubview:sugarLabel];
cell.textLabel.text =firstLabel.text;
} else {
NSString *cellText = [contactSection objectAtIndex:[indexPath row]];
// split the text by the : to get an array containing { "AAA", "BBB" }
NSArray *splitText = [cellText componentsSeparatedByString:@":"];
// form a new string of the form "BBB AAA" by using the individual entries in the array
NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];
firstLabel.tag =40; //This should be a constant probably
//firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
firstLabel.text = contactText;
[firstLabel sizeToFit];
[cell.contentView addSubview:firstLabel];
sugarLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
sugarLabel.tag =40; //This should be a constant probably
//sugarLabel.font = [UIFont boldSystemFontOfSize:16];
sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
[sugarLabel setHidden:YES];
[cell.contentView addSubview:sugarLabel];
NSString *result=[NSString stringWithFormat:@"%@ %@",firstLabel.text,sugarLabel.text];
cell.textLabel.text=result;
}
图2:这是修改按照 BP 建议的代码。结果如下..
编辑:
每当我尝试编写 stringWithFormat 或上述语句时,内存未释放,标签相互重叠。有没有办法解决这个问题..请帮助我..我花了半天多的时间试图解决这个问题,但没有运气
I am trying to add custom labels to my cell in UITableView. When I try to do this the display is messed up and I am unable to figure out what exactly is going on. Please find the image below and I will post the method I wrote..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell.textLabel.font=[UIFont systemFontOfSize:16];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
NSArray *contactSection=[contactNames objectForKey:contact];
NSMutableArray *sugar=[db sugarId];
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
NSArray *splitText = [cellValue componentsSeparatedByString:@":"];
NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
//NSString *result=[contactText stringByAppendingString:[sugar objectAtIndex:[indexPath row]]];
firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];
firstLabel.tag =40; //This should be a constant probably
firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
firstLabel.text = contactText;
[firstLabel sizeToFit];
[cell.contentView addSubview:firstLabel];
sugarLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
sugarLabel.tag =40; //This should be a constant probably
//sugarLabel.font = [UIFont boldSystemFontOfSize:16];
sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
[sugarLabel setHidden:YES];
[cell.contentView addSubview:sugarLabel];
cell.textLabel.text =firstLabel.text;
} else {
NSString *cellText = [contactSection objectAtIndex:[indexPath row]];
// split the text by the : to get an array containing { "AAA", "BBB" }
NSArray *splitText = [cellText componentsSeparatedByString:@":"];
// form a new string of the form "BBB AAA" by using the individual entries in the array
NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];
firstLabel.tag =40; //This should be a constant probably
//firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
firstLabel.text = contactText;
[firstLabel sizeToFit];
[cell.contentView addSubview:firstLabel];
sugarLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
sugarLabel.tag =40; //This should be a constant probably
//sugarLabel.font = [UIFont boldSystemFontOfSize:16];
sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
[sugarLabel setHidden:YES];
[cell.contentView addSubview:sugarLabel];
NSString *result=[NSString stringWithFormat:@"%@ %@",firstLabel.text,sugarLabel.text];
cell.textLabel.text=result;
}
Figure 2: This is modification to the code as suggested by BP. The result is here under..
EDIT:
Whenever I try to write stringWithFormat or the above statement, the memory is not freed and the labels are getting overlap over one other. Is there a way to solve this problem..please help me..I spent more than half a day trying to figure this but no luck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要自定义单元格,我建议您对 UITableViewCell 进行子类化。在您给出的示例代码中,每次表视图请求索引路径中的行的单元格时,您都会创建一个新标签。当您自动释放它们时,您还将它们添加到单元格的内容视图中,这会增加它们的保留计数并确保它们的生命周期与表格视图的生命周期一样长。
随着单元格的重复使用,您将继续向其内容视图添加越来越多的标签,直到最终耗尽内存。
至于奇怪的文本输出,看起来您正在使用格式调用将 UILabel 添加到字符串之一。
如果您执行类似下面代码的操作,您会看到类似的内容。
<代码>
UILabel *label = [[UILabel alloc] init];
NSLog(@"%@", 标签);
[标签发布];
编辑
:看到您编辑的屏幕截图,我假设您向下滚动到“S”部分。在这种情况下,您会看到标签位于其他标签之上。就像我提到的,您每次都会创建一个新标签,并将其放在您第一次(以及第二次、第三次等)使用单元时创建的标签之上。
If you want to have custom cells, I recommend you subclass UITableViewCell. In the example code you have given, you are creating a new label every single time the tableview asks for the cell for a row at an index path. While you are autoreleasing them, you are also adding them the the contentview of the cell, which increases their retain count and ensures that their lifetime is as long as the tableview's.
As cells are reused, you will continue to add more and more labels to it's content view, until you eventually run out of memory.
As far as the strange text output, it looks like you are adding a UILabel to one of your string with format calls.
If you were to do something like the code below, you would see something similar.
UILabel *label = [[UILabel alloc] init];
NSLog(@"%@", label);
[label release];
Edit: Seeing your edited screenshot, I'm assuming you scrolled down to the 'S' section. In which case, you are seeing the labels on top of other labels. Like I mentioned, you are creating a new label every time and putting it on top of the label you created the first (and second, third, etc) time you used the cell.
您显示 UILabel:... 项目的原因是该方法的最后一行,您在其中设置了 cell.textLabel.text。如果您要创建其中包含您自己的字段的单元格,则您不想这样做。
The reason you are getting the UILabel:... items showing up is the last line of the method, where you set the cell.textLabel.text. You don't want to do this if you are creating the cell with your own fields inside it.
应阅读:
您传递的是 UILabel 而不是 NSString
edit
尝试仅在
if (cell == nil) {
块中实例化和添加 UILabels。访问标签属性并在该块之外写入它是可以的
should read:
You are passing in an UILabel and not a NSString
edit
Try to instantiate and add UILabels only in the
if (cell == nil) {
block.while accessing labels properties and writing to it outside this block is ok