自定义UITableViewCell创建问题

时间:2015-08-28 09:06:36

标签: ios objective-c xcode uitableview

概述
我正在使用自定义UITableView创建UITableViewCell(在iOS 8.4中,Objective-C)。我已经创建了自定义单元格中每个项目的IBoutlets。它似乎运作良好,但随机我的UIButton从自定义单元格中消失。

  

这里是我的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



提前致谢告诉我您是否需要更多信息。

4 个答案:

答案 0 :(得分: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 ...(方法)。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

尝试使用这块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
        });
    });

答案 3 :(得分: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]; } 标记,问题就解决了。

bitbucket1:userA/myrepo1
顺便提一下感谢 有价值的提示和建议。