2012-12-02 133 views
8

这是做我的头:-)自定义的UITableViewCell在编辑模式

不动我的UILabels我有一个功能齐全CoreData填充的UITableView一个UIViewController内,我已经成功地实施了“滑动删除选项”(这很简单),我也可以用一个编辑按钮来删除单个实例,在那里出现红圈。

我的问题是,我认为这是因为我有一个CustomCell,当我按下编辑按钮UILabels不会移动到右侧。

我一直在使用别人-(void)layoutSubViews和一些尝试,但没有任何工程。

我已经发布我的代码为我cellForRowAtIndexPath。这是我的应用中注释部分的一部分。此代码有效,我只需要知道如何在进入编辑模式时移动标签?

谢谢你的提示和建议:-)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier = @"Cell"; 

MNCustomCell *cell = [_mainTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
if (cell == nil) { 
    cell = [[MNCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 


//cell.textLabel.text = [_tableArray objectAtIndex:indexPath.row]; 

MNotes *mnotes = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 

cell.noteTitle.text = mnotes.noteTitleString; 
cell.noteSummary.text = mnotes.mainNoteString; 

mnotes.createDate = [[NSDate alloc] init]; 
SORelativeDateTransformer *relativeDateTransformer = [[SORelativeDateTransformer alloc] init]; 
NSString *relativeDate = [relativeDateTransformer transformedValue:mnotes.createDate]; 

cell.noteDate.text = relativeDate; 


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

return cell; 
} 

-

//This is the Custom Cell 
@interface MNCustomCell : UITableViewCell 
{ 

} 

@property (strong, nonatomic) IBOutlet UILabel *noteTitle; 
@property (strong, nonatomic) IBOutlet UILabel *noteDate; 

@property (strong, nonatomic) IBOutlet UITextView *noteSummary; 


@end 

-

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
    // Initialization code 

    [self.contentView addSubview:_noteTitle]; 
    [self.contentView addSubview:_noteSummary]; 
    [self.contentView addSubview:_noteDate]; 

    } 
    return self; 
    } 
+0

是你的标签直接添加到细胞还是你将它们添加到单元格的'contentView'?他们应该在'contentView'中让他们在单元格进入编辑模式或添加其他单元格装饰时自动进行调整。 – rmaddy

+0

我创建了一个新的自定义类并将它们拖入Storyboard中 - 我认为这意味着它们直接位于单元格右侧? –

+0

我已经添加了我的自定义单元的代码 - 我没有在.m文件中添加任何内容 - 现在不需要直到现在。 –

回答

8

另一种解决方案可能会工作,但这种方法会为你自动做动画。 MNCustomCell不会根据单元格的当前状态重新布置视图,但是如果将标签添加到单元格的contentView中,它将会。

下面的例子将移动标签,所以它不会删除按钮干扰。

MNCustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)]]; 
     mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:mainLabel]; 
+0

我已经添加了你在顶部(第三批代码)所说的代码是否是正确的地方?它仍然不起作用。但在TableView中,我仍然设置文本标签,因为我有权利? –

+0

第三批代码?我不确定你是什么意思。 – Fredrik

+0

对不起,如果你看看原来的问题,我已经将我提交的代码分成了三部分。第三个是我指的那个。这是正确的问题的底部。 –

1

实现您的自定义单元格级以下的方法。

- (void)willTransitionToState:(UITableViewCellStateMask)state 

- (void)didTransitionToState:(UITableViewCellStateMask)state 

,并相应地移动你的标签。

应该是这样

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) { 
     label.frame = ... 
    } 
} 

编辑:

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

// label.hidden = YES; 
// label.alpha = 0.0; 
} 

- (void)didTransitionToState:(UITableViewCellStateMask)state { 

    [super didTransitionToState:state]; 

    if (state == UITableViewCellStateShowingDeleteConfirmationMask) { 

     [UIView beginAnimations:@"anim" context:nil]; 
     label.frame = leftFrame; 
     [UIView commitAnimations]; 
    } else if (state == UITableViewCellStateDefaultMask) { 

     [UIView beginAnimations:@"anim" context:nil]; 
     label.frame = rightFrame; 
     [UIView commitAnimations]; 
    } 
} 
+0

您必须更改两种方法中的框架位置。在'will..'方法中,您必须将标签右移,并使用'did..'方法,您必须将标签左移。 – Ilanchezhian

+0

好的 - 我得到它的工作,但标签不与它生成动画,它只是轻弹到新的位置。我试过[UIView beginAnimations:nil context:NULL];但有很多延迟让它看起来很自然。 –

+0

看到我编辑的答案 – Ilanchezhian

相关问题