Пожалуйста, подождите некоторое время, поскольку это длинное объяснение.
У меня есть UIViewController
, который состоит из UIButton
и a UITableView
, который загружает различные типы UITableViewCell
с идентификатором Cell1
и Cell2
, на событие touchUpInside
кнопки. Я использую раскадровку.
Сепаратор для обеих ячеек настраивается.
Cell1
имеет разделитель, который занимает всю ширину ячейки и 1 пиксельную высоту в нижней части ячейки.
В то время как Cell2
имеет разделитель, который имеет смещение 5 пикселей от ячейки, как влево, так и вправо.
При нажатии кнопки tableView
кнопка tableViewCell
заменяется на основе идентификатора ячейки.
Изначально tableView
занимает полную ширину viewController
и состоит из Cell1, но кнопка нажата, tableViewCell
меняются на Cell2, а кадр tableView
изменяется, ширина уменьшается на 10, а x-origin увеличивается на 5.
Но когда это произойдет, разделитель Cell2
находится на расстоянии 5 пикселей от ячейки справа, а слева - на 5 пикселей.
Это происходит для всех Cell2
, которые загружаются данными, а ячейки, у которых нет данных, кадр изменяется соответствующим образом.
Но ячейка после этого имеет ширину Cell1
(большая ширина)
-(void)setSeperatorStyleForTableView :(UITableViewCell *)cell //this is called in cellForRowAtIndex
{
//cell- type of cell(Cell1 or Cell2)
CGRect seperatorFrame;
UIImageView *seperatorImage;
seperatorFrame = [self setSeperatorFrame:cell];
if(firstCellToBeLoaded)//BOOL used to change the button text and load appropriate cells
{
seperatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table_row
2.png"]];
}
else
{
seperatorImage = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"table_row.png"]];
}
seperatorImage.frame = seperatorFrame;
seperatorImage.autoresizingMask = YES;
[cell.contentView addSubview:seperatorImage];
}
//set the customized separator frame
-(CGRect)setSeperatorFrame :(UITableViewCell *)cell
{
CGRect seperatorFrame;
seperatorFrame.size.height = 1.0;
seperatorFrame.origin.y = cell.frame.origin.y + (cell.frame.size.height - 1.0);
if(firstCellToBeLoaded)
{
seperatorFrame.origin.x = cell.frame.origin.x ;
seperatorFrame.size.width = cell.frame.size.width;
}
else
{
seperatorFrame.origin.x = cell.frame.origin.x + 5.0;
seperatorFrame.size.width = cell.frame.size.width -10.0;
}
return seperatorFrame;
}