Changing bounds of imageView of UITableViewCell

前端 未结 5 831

I\'m trying to place various size images inside imageView of UITableViewCell. I get the image data asynch\'ly, create the image, set the content mode of imageView and finall

5条回答
  •  一整个雨季
    2020-12-13 17:08

    So the problem with UITableViewCell's is that you have no control over the size of the built-in objects (namely imageView, contentView, accessoryView, backgroundView). When the table changes, your customizations get trampled over.

    You can, as Behlul pointed out, force the sizes to be correct by using layoutSubviews, but the problem with that is that layoutSubviews is called every time the table scrolls. That is a lot of unnecessary re-layout calls.

    An alternate, method is to add all of your content to the contentView. Similarly if you are customizing the background, you can create a transparent backgroundView and add your custom background view (eg myBackgroundView) as a subview of backgroundView.

    This way you can place and size your items how you want them.

    The down side is the stock messages are no longer received from the accessory or image views. You just have to create you own.

    Hope that helps!

    // This code is not tested
    // MyCustomTableViewCell
    - (id) init{
        self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"MyReuseIdentifier"];
        if(self){
            //image view
            my_image_view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_image.png"]] retain];
            [my_image_view setFrame:CGRectMake(10,10,30,30)];
            [self.contentView addSubview:my_image_view];
    
            //labels
            my_text_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,10,100,15)] retain];
            [self.contentView addSubview:my_text_label];
            //set font, etc
    
            //detail label
            my_detail_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,25,100,15)] retain];
            [self.contentView addSubview:my_detail_label];
            //set font, etc
    
            //accessory view
            //Whatever you want to do here
            //attach "accessoryButtonTapped" selector to button action
    
            //background view
            UIView* background_view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)] autorelease];
            [background_view setBackgroundColor:[UIColor greenColor]];
            background_view.layer.cornerRadius = 17;
            background_view.layer.borderWidth = 3;
            background_view.layer.borderColor = [UIColor whiteColor].CGColor;
    
            [self setBackgroundView:[[[UIView alloc] init] autorelease]];
            [self.backgroundView addSubview:background_view];
        }
    
        return self;
    }
    
    - (void) setLabelText: (NSString*) label_text{
        [my_text_label setText:label_text];
    }
    
    - (void) setDetailText: (NSString*) detail_text{
        [my_detail_label setText: detail_text];
    }
    
    - (void) accessoryButtonTapped{
        //call table view delegate's accessoryButtonTappedForRowWithIndexPath method
    }
    

提交回复
热议问题