修改系统自带cell中控件frame
1 // 要修改cell中控件的frame不能在这个地方,因为这儿方法只负责创建cell,当这个方法返回cell后,系统会自动设置cell中控件的frame。
你在这里设置的frame会被系统设置的覆盖,因此不能在这里设置。那要更改cell中控件的frame怎么办?自定义一个cell,并重写layoutSubviews方法,
在- (void)layoutSubviews方法中设置控件的frame。 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 5 6 if (!cell) { 7 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 8 } 9 10 return cell; 11 }
1 // 在这里设置frame 2 - (void)layoutSubviews 3 { 4 [super layoutSubviews]; 5 6 // 调整子控件的frame 7 CGFloat imageX = 10; 8 CGFloat imageY = 10; 9 CGFloat imageH = self.height - 2 * imageY; 10 CGFloat imageW = imageH * 200 / 112; 11 self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH); 12 13 self.textLabel.x = CGRectGetMaxX(self.imageView.frame) + 10; 14 15 self.detailTextLabel.x = self.textLabel.x; 16 17 }
com.nigo
浙公网安备 33010602011771号