就个人而言,我会创建一个自定义表格单元格并执行以下操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ImageCell";
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureWithRow:indexPath.row];
}
然后在您的自定义表格单元格中,为您的可调整大小的图像视图创建一个 IBOutlet 并添加一个方法:
-(void)configureWithRow:(NSInteger)row
{
if(indexPath.row %2)
{
self.resizableImageView.frame = CGRectMake(200,70,50,50)
}
else
{
cell.resizableImageView.frame = CGRectMake(100,70,50,50)
}
}
这做了几件事
您不必检查 nil 单元格,因为只要标识符与情节提要中的标识符匹配并且您的视图控制器是 UITableViewController,就可以保证获得一个单元格返回 dequeueReusableCellWithIdentifier。
您将单元格配置的逻辑移动到自定义表格视图单元格,这是它真正应该在的地方。视图控制器不应该关心单元格中图像的大小。