【发布时间】:2013-02-08 07:41:40
【问题描述】:
请稍等,因为这是一个很长的解释
我有一个UIViewController,它由一个UIButton 和一个UITableView 组成,它在按钮的事件touchUpInside 上加载带有标识符Cell1 和Cell2 的不同类型的UITableViewCells。我正在使用故事板。
两个单元格的分隔符都是自定义的。
Cell1 有一个分隔符,占据整个单元格的宽度和单元格底部的 1 个像素高度。
而 Cell2 有一个分隔符,它与单元格的偏移量为 5 个像素,左右都有。
每次单击tableView 之外的按钮时,tableViewCells 都会根据单元标识符进行交换。
最初tableView占据viewController的完整宽度,由Cell1组成,但点击按钮后,tableViewCells变为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;
}
【问题讨论】:
标签: ios objective-c