自定义的UITableViewCell创建问题

问题描述:

概述
我创建一个UITableView定制UITableViewCell(iOS中8.4,Objective-C的)。我在Custom Cell中创建了每个项目的IBoutlets。它似乎工作得很好,但随机我的UIButton从自定义单元中消失。
自定义的UITableViewCell创建问题

这里我的cellForRowAtIndexPath代码

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    linkCell*cell = (linkCell*)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    if(!cell){ 
     cell = [[linkCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    cell.contentView.backgroundColor = [UIColor clearColor]; 
    NSDictionary *aDict = [numberOfSections objectAtIndex:indexPath.section]; 
    NSDictionary* info = [[aDict objectForKey:@"Data"]objectAtIndex:indexPath.row]; 

    static float span = 5; 
    static float dateviewsize = 83; 
    static float linesize = 1; 
    static float descriptionsize = 65; 

    //DAY_NAME 
    NSString* string = [self getDayNameFromDate:[info valueForKey:@"copied_url_date"]]; 
    cell.lblDayName.text = string; 

    //DAY_NUMBER 
    string = [self getDayNumberFromDate:[info valueForKey:@"copied_url_date"]]; 
    cell.lblDayNumber.text = string; 

    //DAY_TIME 
    string = [self getTimeFromDate:[info valueForKey:@"copied_url_date"]]; 
    cell.lblDayTime.text = string; 

    //Link Label 
    if([[info objectForKey:@"copied_url_title"] isEqualToString:@"nil"]){ 
     string = [NSString stringWithFormat:@"%@",[info valueForKey:@"copied_url"]]; 
     cell.lblLink.font = [UIFont fontWithName:FONT_LIGHT size:14]; 
     cell.lblLink.textColor = [UIColor colorWithRed:51.0/255.0 green:0/255.0 blue:135.0/255.0 alpha:1.0]; 
    }else{ 
     string = [NSString stringWithFormat:@"%@",[info objectForKey:@"copied_url_title"]]; 
     cell.lblLink.font = [UIFont fontWithName:FONT_MEDIUM size:16]; 
     cell.lblLink.textColor = [UIColor darkGrayColor]; 
    } 
    cell.lblLink.text = string; 

    float w,h,y; 
    w = self.view.frame.size.width-(span*2); 
    CGRect dataviewframe,titleRect,buttonviewrect,imageRect; 
    CGSize newsize; 
    [cell.lblDescreption setFont:[UIFont fontWithName:FONT_LIGHT size:16]]; 
    [cell.lblDescreption setNumberOfLines:3]; 
    [cell.linkimage setHidden:NO]; 

    if([AFNetworkReachabilityManager sharedManager].isReachable){ 
     //Unspacified ImageView 
     if([[info objectForKey:@"copied_url_img"]isEqualToString:@"nil"]){ 
      [cell.linkimage setHidden:YES];//hide image 
      //make call to get image & title of Copied URL 
      [self MakeCallAndSetImageToImageView:indexPath ToGetImageFromURL:[info valueForKey:@"copied_url"]]; 

      //new size of link title 
      y = span+dateviewsize+span+linesize+1; 
      if([[info objectForKey:@"copied_url_description"] isEqualToString:@"nil"]){ 
       [cell.lblDescreption setHidden:YES];//hide description 
       //set dataview's new height 
       h = span+dateviewsize+span; 
      } 
      else{ 
       [cell.lblDescreption setHidden:NO];//show description 
       NSString* string = [NSString stringWithFormat:@"%@",[info objectForKey:@"copied_url_description"]]; 
       [cell.lblDescreption setText:string]; 
       newsize = [self getStringHeightWithMaxWidth:w-(span*2) AndText:cell.lblDescreption]; 
       if(newsize.height > descriptionsize){ 
        titleRect = CGRectMake(span,y,w-(span*2),descriptionsize); 
        //set dataview's new height 
        h = y+descriptionsize+span; 
       }else{ 
        titleRect = CGRectMake(span,y,w-(span*2),newsize.height); 
        //set dataview's new height 
        h = y+newsize.height+span; 
       } 
       [cell.lblDescreption setFrame:titleRect]; 

      } 
      dataviewframe = CGRectMake(span,span,w,h); 

      //set buttonview's new frame 
      buttonviewrect = CGRectMake(span,h+span+2,w,50); 

     }else{ 
      [cell.linkimage setHidden:NO];//show image 
      //new size of link image 
      y = span+dateviewsize+span+linesize+1; 
      float imageH = [self SizeWithScaledToWidth:w-(span*2)].height; 
      imageRect = CGRectMake(span,y,w-(span*2),imageH); 
      NSString* imgurl=[NSString stringWithFormat:@"%@",[info objectForKey:@"copied_url_img"]]; 
      [cell.linkimage setImageWithURL:[NSURL URLWithString:imgurl]]; 
      [cell.linkimage setFrame:imageRect]; 
      y = y+imageH+span; 
      //new size of link title 
      if([[info objectForKey:@"copied_url_description"] isEqualToString:@"nil"]){ 
       [cell.lblDescreption setHidden:YES]; 
       h = y+span; 
      }else{ 
       [cell.lblDescreption setHidden:NO];//show description 
       NSString* string = [NSString stringWithFormat:@"%@",[info objectForKey:@"copied_url_description"]]; 
       [cell.lblDescreption setText:string]; 
       newsize = [self getStringHeightWithMaxWidth:w-(span*2) AndText:cell.lblDescreption]; 
       if(newsize.height > descriptionsize){ 
        titleRect = CGRectMake(span,y,w-(span*2),descriptionsize); 
        //set dataview's new height 
        h = y+descriptionsize+span; 
       }else{ 
        titleRect = CGRectMake(span,y,w-(span*2),newsize.height); 
        //set dataview's new height 
        h = y+newsize.height+span; 
       } 
       [cell.lblDescreption setFrame:titleRect]; 
      } 
      //set dataview's new frame 
      dataviewframe = CGRectMake(span,span,w,h); 

      //set buttonview's new frame 
      buttonviewrect = CGRectMake(span,h+span+2,w,50); 
     } 
    }else{ 
     //NO INTERNET CONNECTION 
     //hide link image 
     [cell.linkimage setHidden:YES]; 

     //hide link title 
     [cell.lblDescreption setHidden:YES]; 

     //set dataview's new frame 
     h = span+dateviewsize; 
     dataviewframe = CGRectMake(span,span,w,h); 

     //set buttonview's new frame 
     buttonviewrect = CGRectMake(span,h+span+2,w,50); 
    } 
    cell.dataview.frame = dataviewframe; 
    cell.buttonview.frame = buttonviewrect; 
    if([selectedRow isEqual:indexPath]){ 
     cell.buttonview.alpha=1; 
    }else{ 
     cell.buttonview.alpha=0; 
    } 
    [cell.btnShare setHidden:NO]; 
    [cell.buttonview bringSubviewToFront:cell.btnShare]; 
    [cell.buttonview bringSubviewToFront:cell.btnOpen]; 
    [cell.buttonview bringSubviewToFront:cell.btnCopy]; 
    [cell.buttonview bringSubviewToFront:cell.btnDelete]; 

    [cell.btnShare addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btnOpen addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btnCopy addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btnDelete addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.btnShare setTag:indexPath.row]; 
    [cell.btnOpen setTag:indexPath.row]; 
    [cell.btnCopy setTag:indexPath.row]; 
    [cell.btnDelete setTag:indexPath.row]; 

    return cell; 
} 

这里是我的屏幕截图
enter image description here
enter image description here



在此先感谢告诉我是否需要更多信息。

+0

删除并尝试'if(!cell){cellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”]; }' –

+0

将表格视图单元格作为自定义单元格 –

经过很长时间,我发现我的代码中有错误。我正在设置buttonview标记indexPath.row并且还给button标记了相同的标记。当DidSelectRowAtIndexPath被调用,我在做什么,

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    linkCell*cell = (linkCell*)[tableView cellForRowAtIndexPath:indexPath]; 
    if([selectedRow isEqual:indexPath]){ 
     selectedRow = nil; 
//this is where i'm mistaking. 
     [[cell viewWithTag: indexPath.row]setAlpha:1]; 
    }else{ 
     selectedRow = indexPath; 
//this is where i'm mistaking. 
     [[cell viewWithTag: indexPath.row]setAlpha:0]; 
    } 
    [tableView reloadData]; 
} 

所以,我给-1标签buttonview,问题就解决了。

if([selectedRow isEqual:indexPath]){ 
      selectedRow = nil; 
      [[cell viewWithTag: -1]setAlpha:1]; 
     }else{ 
      selectedRow = indexPath; 
      [[cell viewWithTag: -1]setAlpha:0]; 
     } 

的方式感谢宝贵提示,并建议。

你在-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中做得太多了。给定制单元一个配置方法,比如-(void)configureCell,这个单元的内容和布局将被定义是一个很好的习惯。换句话说,让单元负责(尽可能)其内容和布局而不是tableview。

我想这个部分会导致问题:

if([AFNetworkReachabilityManager sharedManager].isReachable) { 
    ... 
} 

特别:

[self MakeCallAndSetImageToImageView:indexPath ToGetImageFromURL:[info valueForKey:@"copied_url"]]; 

我不知道这种方法的实现,但我有你要看看那边的感觉,因为它可能是检索您请求的图像时的线程问题。

另一个提示:您命名类和方法的方式并不总是遵循Objective-C约定。检查linkCell(班级)和MakeCallAndSetImageToImageView ...(方法)。

可能的布局破裂。尝试捕获视图层次结构。在Xcode中:调试>查看调试>捕获视图层次结构。

此外,方法dequeueReusableCellWithIdentifier:forIndexPath:始终返回有效的单元格。如果(!cell){...}永远不会被执行,您可以将其删除。

尝试使用这块GCD,我认为它会工作。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     //Do data fetching and calculation 
     dispatch_async(dispatch_get_main_queue(), ^(void) { 

      //Update UI on main thread 
     }); 
    }); 
+0

我应该在哪里使用这个GCD块? – Vats

+0

Vats,我认为这取决于你想在哪里使用,也就是说,你可以在你想要使用后台线程和主线程的地方使用它,正如我所提到的那样,你可以在DISPATCH_QUEUE_PRIORITY_DEFAULT中进行计算和获取数据并更新主线程上的ui。 –

+0

谢谢你,Gopal Devra – Vats