如何在表格视图单元格

时间:2015-05-16 13:46:37

标签: ios

我的原型单元格中有一个自定义UIView在iOS 8.2应用程序之前,它是一个普通的UIImage,我用[currentCell viewWithTag:200]将它恢复正常;在我的UITableView cellForRowIndexPath方法中。但是在我制作了自定义uiview并替换了uiimage后,它总是返回nil ..

修改的 在尝试将新标签添加到工作菜单控制器之后,更简单的方法是循环遍历字符串数组。并设置标签和循环(已经为菜单打印正确的值)它找不到新添加的标签。有可能以某种方式不能编译标签?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Lists *listItem = [lists objectAtIndex:indexPath.row];

if(currentCell == nil){
    currentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

//THIS ALLWAYS RETUNS nil
ShadedBadgeImage *cellImage = (ShadedBadgeImage *)[currentCell viewWithTag:200];
//UIView *cellImage = (UIView *)[currentCell viewWithTag:200];
//cellImage.completedProcentage = 2.0 * listItem.id.doubleValue;


UILabel *cellTitle = (UILabel *) [currentCell viewWithTag:101];
UILabel *cellDescription = (UILabel *)[currentCell viewWithTag:102];

cellTitle.text = listItem.id.stringValue;
cellDescription.text = listItem.shortText;

return currentCell;

我的自定义uiview。即使用layoutSubviews渲染蒙版图像和进度条。

IB_DESIGNABLE

@implementation ShadedBadgeImage {

    double lineWidth;
    CAShapeLayer *backgroundLayer;
    CAShapeLayer *coloredLayer;
    CALayer *imageLayer;

}
    - (void)baseInit {
        lineWidth = 5.0;
        [self updateStroke];
    }

    - (id)initWithFrame:(CGRect)frame
    {

        self = [super initWithFrame:frame];
        if (self) {
            [self baseInit];
        }
        return self;
    }

    - (id)initWithCoder:(NSCoder *)aDecoder {
        if ((self = [super initWithCoder:aDecoder])) {
            [self baseInit];
        }
        return self;
    }

    - (void)layoutSubviews {
        [super layoutSubviews];

        //Adds the background ring layer
        if(backgroundLayer == nil)
        {
            backgroundLayer = [[CAShapeLayer alloc] init];

            CGRect rect = CGRectInset(self.bounds, lineWidth/2.0, lineWidth/2.0);
            UIBezierPath *roundPath = [UIBezierPath bezierPathWithOvalInRect:rect];

            backgroundLayer.path = roundPath.CGPath;
            backgroundLayer.fillColor = nil;
            backgroundLayer.lineWidth = lineWidth;
            backgroundLayer.strokeColor = [UIColor colorWithWhite:0.5 alpha:0.05].CGColor;

            [self.layer addSublayer:backgroundLayer];
        }
        backgroundLayer.frame = self.layer.frame;

        //Adds the color ring layer
        if(coloredLayer == nil)
        {
            coloredLayer = [[CAShapeLayer alloc] init];

            CGRect rect = CGRectInset(self.bounds, lineWidth/2.0, lineWidth/2.0);
            UIBezierPath *roundPath = [UIBezierPath bezierPathWithOvalInRect:rect];

            coloredLayer.path = roundPath.CGPath;
            coloredLayer.fillColor = nil;
            coloredLayer.lineWidth = lineWidth;
            coloredLayer.strokeColor = [UIColor blueColor].CGColor;

            coloredLayer.anchorPoint = CGPointMake(0.5, 0.5);
            coloredLayer.transform = CATransform3DRotate(coloredLayer.transform, -M_PI/2, 0, 0, 1);

            [self.layer addSublayer:coloredLayer];
        }
        coloredLayer.frame = self.layer.frame;

        //Adds the color ring layer
        if(imageLayer == nil)
        {
            CAShapeLayer *imageMask = [[CAShapeLayer alloc] init];
            CGRect insertBounds = CGRectInset(self.bounds, lineWidth + 3.0, lineWidth + 3.0);
            UIBezierPath *innerPath = [UIBezierPath bezierPathWithOvalInRect:insertBounds];

            imageMask.path = innerPath.CGPath;
            imageMask.fillColor =  [UIColor grayColor].CGColor;
            imageMask.frame = self.bounds;
            [self.layer addSublayer:imageMask];


            imageLayer = [[CALayer alloc] init];
            imageLayer.mask = imageMask;
            imageLayer.frame = self.layer.frame;
            imageLayer.backgroundColor =[UIColor grayColor].CGColor;
            imageLayer.contentsGravity = kCAGravityResizeAspectFill;

            [self.layer addSublayer:imageLayer];
        }
        [self updateStroke];
    }

    - (void) updateStroke{
        if (coloredLayer != nil)
        {
            coloredLayer.strokeEnd = self.completedProcentage;
        }

        if(self.image != nil)
        {
            imageLayer.contents = (__bridge id)([self.image CGImage]);
        }
    }   
    @end

1 个答案:

答案 0 :(得分:0)

正如评论所说,不要使用标签去自定义UITableCellViews以获得更多控制