2014-09-24 96 views
0

我有一张表。表有一个简单的自定义单元格。这个单元格中只有标签。 这里是cellForRow功能的代码:无法更改UITableviewCell子类中的子视图位置

MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

cell.myLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
cell.myLabel.frame = CGRectOffset(cell.myLabel.frame, 0, -20); 
cell.myLabel.backgroundColor = [UIColor redColor]; 

return cell; 

一切工作正常,除了这一点:

cell.myLabel.frame = CGRectOffset(cell.myLabel.frame, 0, -20); 

标签不想动! 我在iOS8中发现了这个问题。

任何人的帮助,请:)

MyCell.m

回答

0

文件添加layoutSubviews方法,改变myLabel框架

- (void)layoutSubviews { 
// always try to set frame in layoutSubviews 
[super layoutSubviews]; 
self.myLabel.frame = CGRectOffset(self.myLabel.frame, 0, -20); 
} 

,并请修改这样

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

static NSString *cellIdentifier; 
cellIdentifier = @"Cell"; 

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell == nil) { 
    cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    cell.myLabel.backgroundColor = [UIColor redColor]; 
} 
cell.myLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 

return cell; 

} 
代码
+0

的感谢!它解决了我的问题,但部分解决了。如果我scrolldown,然后新的单元格出现与标签,不动:( – 2014-09-25 15:59:20

+0

修改我的答案检查一次 – 2014-09-25 16:47:45

+0

对不起,我需要使用故事板中的表格原型,因此使用... if(cell == nil) ......不适合我的情况 – 2014-09-30 16:06:37