0

I have a table. Table has 1 simple custom cell. There is only label in this cell. Here is code of cellForRow function:

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;

Everything works fine except this:

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

Label don't want to move! I found this problem in iOS8.

Anybody help please :)

Kirill Pyulzyu
  • 381
  • 2
  • 11

2 Answers2

5

Ok, I found the answer here https://stackoverflow.com/a/13539782/1928161 :)

The trick is to add

cell.myLabel.translatesAutoresizingMaskIntoConstraints=YES;
Community
  • 1
  • 1
Kirill Pyulzyu
  • 381
  • 2
  • 11
0

in MyCell.m file add layoutSubviews method and change myLabel frame

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

and please modify your code like this

-(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;

}
Anjaneyulu Battula
  • 1,910
  • 16
  • 33