【问题标题】:iOS 6: UITableView Edit and AutoresizingiOS 6:UITableView 编辑和自动调整大小
【发布时间】:2012-09-19 00:52:33
【问题描述】:

嗨,有人可以帮助我了解如何在 iOS 6.0 中编辑时自动布局表格单元格的组件吗?我已将 UITableViewCell Autosizing 选项的 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");
    }
}

当用户单击左圆形按钮时,是否有在该按钮单击时添加选择器并获取单元格索引?

【问题讨论】:

    标签: iphone ios uitableview ios6 autoresize


    【解决方案1】:

    当您不使用 AutoLayout 时,您的 ratingImageView 使用自动调整大小的掩码来调整自身的大小和位置。默认情况下,这意味着到左侧和顶部的距离是固定的,大小不会改变。

    当您切换单元格以编辑 contentView 移动和调整大小但您的 ratingImageView 保持固定在顶部和左侧时。您可以通过临时(出于学习目的)为单元格 contentView 设置背景颜色来可视化这一点,并查看它在您编辑和删除单元格时如何调整大小。

    您希望评分视图与右边缘而不是左边缘保持固定距离。您可以通过设置 ratingImageView 的 autoresizingMask 属性在 InterfaceBuilder(在其中执行 XIB 或 Storyboard)或代码中更改此设置。


    转到此标签

    像这样改变自动调整大小

    或者在代码中实现

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

    【讨论】:

    • 我确实将右上角设置为 UITableViewController 而不是 imageview!我所有的方法都浪费了!!!不敢相信事情就这么简单。非常感谢!但只是想知道当我单击左圆形按钮编辑单元格时是否有任何方法被调用?这是我唯一想不通的事情。
    • 如果我没记错你的单元格将被发送到- (void) willTransitionToState:(UITableViewCellStateMask) state 并带有UITableViewCellStateShowingDeleteConfirmationMask 状态。默认状态和编辑状态调用相同的方法。 (查看文档)
    • 我的意思是我设置了 UITableViewCell 的右上角,但它不起作用!
    猜你喜欢
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多