首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >tableview单元格单击的数据重叠

tableview单元格单击的数据重叠
EN

Stack Overflow用户
提问于 2012-02-09 18:02:45
回答 1查看 471关注 0票数 0

我有一个tableView,它包含it.if格式的解析数据,我单击我的表视图,表视图中的cell.the文本标签被覆盖,它在it.below上显示了一个黑点,是屏幕截图和代码。

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle      reuseIdentifier:CellIdentifier] autorelease];

 }
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];
NSInteger n=[str intValue];
NSLog(@"the value:%@",str);
if(n ==0)
{   
CGRect starFrame = CGRectMake(217,6, 85, 40);

UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:@"nostar.png"];
starImage.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:starImage];

NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
CGRect labelFrame = CGRectMake(203,18, 20, 20);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=boo;
[cell.contentView addSubview:Label];


  }
 if(n >=1)
 {   
   CGRect starFrame = CGRectMake(217,6, 85, 40);

  UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
   starImage.image = [UIImage imageNamed:@"1star.png"];
  [cell.contentView addSubview:starImage];

   NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
   CGRect labelFrame = CGRectMake(203,18, 20, 20);
   UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
   Label.text=boo;
    [cell.contentView addSubview:Label];

    }

if(n >=2)
{   
CGRect starFrame = CGRectMake(217,6, 85, 40);

UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:@"twostar.png"];
[cell.contentView addSubview:starImage];

NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
CGRect labelFrame = CGRectMake(203,18, 20,20);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=boo;
[cell.contentView addSubview:Label];

 }
if(n >=3)
{   
CGRect starFrame = CGRectMake(217,6, 85, 40);

UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:@"threestar.png"];
[cell.contentView addSubview:starImage];

NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
CGRect labelFrame = CGRectMake(203,18, 20,20);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=boo;
[cell.contentView addSubview:Label];

 }

 if(n >= 4)
 {
CGRect starFrame = CGRectMake(217,6, 85, 40);

UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:@"4star.png"];
[cell.contentView addSubview:starImage];

NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
CGRect labelFrame = CGRectMake(203,18,20,20);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=boo;
[cell.contentView addSubview:Label];
}
  if(n >= 5)
  {
CGRect starFrame = CGRectMake(217,6, 85, 40);

UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:@"5star.png"];
[cell.contentView addSubview:starImage];

NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
CGRect labelFrame = CGRectMake(203,18, 20,20);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=boo;

[cell.contentView addSubview:Label];
}


  cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
  cell.textLabel.text=[self.story objectAtIndex:indexPath.row];
  cell.textLabel.numberOfLines=2;

  return cell;
  }

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath {

 title=[self.story objectAtIndex:indexPath.row];
NSDictionary *detaildesc1=[self.descriptiondesc objectAtIndex:indexPath.row];
FirstViewDetailDetail *detailViewController1 = [[FirstViewDetailDetail alloc]     initWithItem:detaildesc1 Title:title];    
    [self.navigationController pushViewController:detailViewController1 animated:YES];
   [detailViewController1 release];

   }

EN

Stack Overflow用户

发布于 2012-02-09 21:13:03

您的问题是每次显示一个单元格时都会添加一个新的标签和图像视图。

您应该只这样做一次,当您必须创建一个新的单元时,因为您没有从未使用的单元队列中获得一个单元。

“黑点”是一对叠加在一起的UILabels。上下滚动表格以查看相同的内容。

将您的UI*分配移动到if (cell == nil)中,并在该if语句之外配置它们。

如下所示:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // create a new cell
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        // create views here and add them to the cell. 
        CGRect starFrame = CGRectMake(217,6, 85, 40);
        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        // use tags to get the views later.
        starImage.tag = 1021;
        starImage.image = [UIImage imageNamed:@"nostar.png"];
        starImage.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:starImage];

        CGRect labelFrame = CGRectMake(203,18, 20, 20);
        UILabel *label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
        label.tag = 1022;
        [cell.contentView addSubview:label];

        // do all cell configuration here that is the same for all cells in your table
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.numberOfLines=2;
    }
    // whatever happened before you have a valid cell at this point

    // get the imageView and label you added before
    UIImageView *starImageView = [cell.contentView viewWithTag:1021];
    UILabel *label = [cell.contentView viewWithTag:1022];

    NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
    NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];
    NSInteger n=[str intValue];
    UIImage *image = nil;
    switch (n) {
        case 0: 
            image = [UIImage imageNamed:@"nostar.png"];
            break;
        case 1: 
            image = [UIImage imageNamed:@"1star.png"];
            break;
        case 2: 
            image = [UIImage imageNamed:@"twostar.png"];
            break;
        case 3: /* more code here */
        case 4: /* here */
        case 5: /* and here */
    }
    starImageView.image = image;
    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
    label.text = boo;

    cell.textLabel.text=[self.story objectAtIndex:indexPath.row];
    return cell;
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9208762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档