Iphone iOS 6:UITableView编辑和自动调整大小

Iphone iOS 6:UITableView编辑和自动调整大小,iphone,ios,uitableview,ios6,autoresize,Iphone,Ios,Uitableview,Ios6,Autoresize,您好,有人能帮我理解在iOS 6.0中编辑时如何自动布局表格单元格的组件吗?我在属性检查器中将UITableViewCell自动调整大小选项的AutoLayout设置为FALSE!单元格的右侧图像视图由删除按钮重叠。请参考所附图片。下面是这方面的代码。是否有其他方法可以解决此问题 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

您好,有人能帮我理解在iOS 6.0中编辑时如何自动布局表格单元格的组件吗?我在属性检查器中将UITableViewCell自动调整大小选项的AutoLayout设置为FALSE!单元格的右侧图像视图由删除按钮重叠。请参考所附图片。下面是这方面的代码。是否有其他方法可以解决此问题

- (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];
    }     
}
我添加了以下委托方法,当我滑动单元格时,它可以正常工作

-(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会固定在顶部和左侧。您可以通过临时(出于学习目的)为“单元格内容”视图设置背景色,并查看在编辑和删除单元格时其大小如何调整来可视化此内容

-(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;
}
您希望评级视图与右边缘保持固定距离,而不是与左边缘保持固定距离。您可以在InterfaceBuilder(在其中执行XIB或故事板)或通过设置ratingImageView的autoresizingMask属性在代码中对此进行更改


转到此选项卡

像这样更改自动调整大小

或者用代码来做

// in code you specify what should be flexible instead of what should be fixed...
[ratingImageView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];

我确实将右上角设置为UITableViewController而不是imageview!!我所有的方法都浪费了!!!真不敢相信这么简单。非常感谢你!但我只是想知道,当我点击左圆形按钮编辑单元格时,是否有任何方法被调用?这是我唯一想不出来的。如果我没记错的话,你的手机将被发送
-(void)willTransitionOnState:(UITableViewCellStateMask)state
uitableViewCellStatesShowingDeleteConfirmationMask
状态。默认状态和编辑状态调用相同的方法。(查看文档)我的意思是,我已将右上角设置为UITableViewCell,但它没有;不行!