2012-09-27 42 views
4

嗨有人能帮我理解如何在iOS 6.0中编辑时自动布局表格单元的组件?我已将AutoLayout FALSE设置为在属性检查器中右上角的UITableViewCell自动调整选项!单元格的右侧图像视图与删除按钮重叠。请参照附图。以下是此代码。无论如何,我可以解决这个问题吗?iOS 6:UITableView编辑和自动修复

- (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按钮时,这个方法不会被调用!真奇怪!我错过了一些东西。我添加了以下方法,当按下Edit/Done按钮时被调用,但没有办法检测将被选中编辑的单元格!

- (void)setEditing:(BOOL)editing animated:(BOOL)animate 
{ 
    [super setEditing:editing animated:animate]; 
    if(editing) 
    { 
     NSLog(@"editMode on"); 
    } 
    else 
    { 
     NSLog(@"Done leave editmode"); 
    } 
} 

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

Image

回答

6

你ratingImageView使用自动尺寸口罩来调整,当你不使用自动版式摆正自己的位置。默认情况下,这意味着到左侧和顶部的距离是固定的,尺寸不会改变。

当您切换单元格以编辑contentView时移动并调整大小,但您的ratingImageView保持固定在顶部和左侧。您可以暂时(为学习目的)为单元格contentView设置背景颜色,并在编辑和删除单元格时查看它的大小调整。

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


转到此选项卡

Go to this tab

更改自动调整大小这样

enter image description here

还是做在代码

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

我没有设置右上UITableViewController不Imageview!我所有的方法都浪费!不能相信这是这么简单。非常感谢你!但只是想知道是否有任何方法,当我点击左键按钮编辑单元格时被调用?这是我无法想象的唯一的事情。 – applefreak

+0

如果我不记得有错,你的单元格会通过UITableViewCellStateShowingDeleteConfirmationMask状态发送' - (void)willTransitionToState:(UITableViewCellStateMask)状态'。同样的方法被称为默认状态和编辑状态。 (看文档) –

+0

我的意思是我已经设置了正确的UITableViewCell哪个did not; t工作! – applefreak