Draw a Custom cell for tableview (UITableView), with changed colors and separator color and width

后端 未结 3 2016
囚心锁ツ
囚心锁ツ 2020-12-09 07:21

I want to draw the background of a UITableViewCell which has a grouped style. The problem with me is I am not able to call the -(void)drawRect:(CGRect)rect or I

3条回答
  •  鱼传尺愫
    2020-12-09 07:43

    HI GUYS,

    I have got this code working....

    just did this.....

    In CustomCellBackGroundView

    - (BOOL) isOpaque {
        return NO;
    }
    
    
    
    - (id)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            [self setFillColor:[UIColor colorWithRed:0.07 green:.23 blue:.48 alpha:0.5]];
            [self setBorderColor:[UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:0.5]];
            [self setClearsContextBeforeDrawing:YES];
        }
        return self;
    }
    
    - (void)setNeedsDisplay{
        [self setNeedsDisplayInRect:self.frame];
    }
    

    and in ur UITableViewController class just do this.....

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *MyIdentifier = @"My Identifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if(cell==nil){
    
            cell = [self reuseTableViewCellWithIdentifier:MyIdentifier indexPath:indexPath];
    
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
            [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        }
    
        CustomCellBackgroundView *custview = [[CustomCellBackgroundView alloc] initWithFrame:CGRectMake(0, 0, 320, 35)];
    
        if(([tbleView numberOfRowsInSection:indexPath.section]-1) == 0){
            custview.position = CustomCellBackgroundViewPositionTop;
        }
        else if(indexPath.row == 0){
            custview.position = CustomCellBackgroundViewPositionTop;
        }
        else if (indexPath.row == ([tbleView numberOfRowsInSection:indexPath.section]-1)){
            custview.position  = CustomCellBackgroundViewPositionBottom;
        }
        else{
            custview.position = CustomCellBackgroundViewPositionMiddle;
        }
        [cell setBackgroundView:custview];
        [custview release];
    
    
        UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:101];
    
    
    
            [lbl setText:@"Hurray and thanks to all folks"];
    
        //[cell setNeedsDisplay];
        return cell;
    }
    

    remeber dont do this in (cell==nil) block and give special thanks to all folks at

    How to customize the background/border colors of a grouped table view cell?

提交回复
热议问题