您好有人可以帮助我了解如何在 iOS 6.0 中编辑时自动布局表格单元格的组件吗?我已将 AutoLayout FALSE 设置为 UITableViewCell Autosizing 选项设置为属性检查器中的右上角!单元格的右侧 ImageView 通过删除按钮重叠。请引用附图。以下是此代码。无论如何我可以解决这个问题吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name;
cell.gameLabel.text = player.game;
cell.ratingImageView.image = [self imageForRating:player.rating];
return cell;
}
- (UIImage *)imageForRating:(int)rating
{
switch (rating)
{
case 1: return [UIImage imageNamed:@"1StarSmall.png"];
case 2: return [UIImage imageNamed:@"2StarsSmall.png"];
case 3: return [UIImage imageNamed:@"3StarsSmall.png"];
case 4: return [UIImage imageNamed:@"4StarsSmall.png"];
case 5: return [UIImage imageNamed:@"5StarsSmall.png"];
}
return nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.players removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
我添加了以下委托(delegate)方法,当我滑动单元格时它工作正常。
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = YES;
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = NO;
}
...但是当我按下 editButtonItem 按钮时不会调用此方法!那很奇怪!我错过了一些东西。我添加了以下方法,当按下“编辑/完成”按钮时会调用该方法,但无法检测将被选中进行编辑的单元格!
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing)
{
NSLog(@"editMode on");
}
else
{
NSLog(@"Done leave editmode");
}
}
当用户点击左侧圆形按钮时,是否可以在该按钮点击上添加选择器并获取单元格索引?
最佳答案
当您不使用 AutoLayout 时,您的 ratingImageView 使用自动调整大小掩码来调整自身的大小和位置。默认情况下,这意味着到左侧和顶部的距离是固定的,并且大小不会改变。
当您切换单元格以编辑 contentView 移动和调整大小但您的 ratingImageView 保持固定在顶部和左侧。您可以通过临时(出于学习目的)为单元格 contentView 设置背景颜色来可视化这一点,并查看它在您编辑和删除单元格时如何调整大小。
您希望评分 View 与右边缘而不是左边缘保持固定距离。您可以在 InterfaceBuilder(您执行 XIB 或 Storyboard的地方)或通过设置 ratingImageView 的 autoresizingMask 属性在代码中更改它。
转到此选项卡
像这样更改自动调整大小
或者用代码实现
// in code you specify what should be flexible instead of what should be fixed...
[ratingImageView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
关于iphone - iOS 6 : UITableView Edit and Autoresizing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12617950/