【问题标题】:Custom UITableViewCell in edit mode does not move my UILabels编辑模式下的自定义 UITableViewCell 不会移动我的 UILabel
【发布时间】:2012-11-19 21:52:31
【问题描述】:

这让我很头疼:-)

我在 UIViewController 内有一个功能齐全的 CoreData Populated UITableView,我已经成功实现了“滑动删除选项”(这很容易),我还可以使用红色圆圈处的编辑按钮删除单个实例东西出现了。

我的问题是,我认为这是因为我有一个 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;
  }

【问题讨论】:

  • 您的标签是直接添加到单元格还是添加到单元格的contentView?它们应该在contentView 中,以便在单元格进入编辑模式或添加其他单元格装饰时自动调整。
  • 我创建了一个新的自定义类并将它们从情节提要中拖到其中 - 我认为这意味着它们直接在单元格上,对吧??
  • 我已经为我的自定义单元格添加了代码 - 我没有在 .m 文件中添加任何内容 - 直到现在都不需要。
  • 我不使用Interface Build,所以我不知道该怎么做。我用代码做所有事情。对不起。
  • 代码很好 :-) 我可以同时使用两者,代码会更好,这样我就可以理解幕后发生的事情:-)

标签: iphone ios uitableview edit custom-cell


【解决方案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];
    }
}

【讨论】:

  • 您只需要在这两种方法中更改框架位置即可。在will.. 方法中,您必须将标签向右移动,而在did.. 方法中,您必须向左移动标签。
  • 好的 - 我得到了它的工作,但是标签没有动画,它只是轻弹到新的位置。我试过 [UIView beginAnimations:nil context:NULL];但是要让它看起来自然要延迟很多。
  • 对不起,我刚刚看到你的答案 - 吃过晚餐:-) 我可以移动标签的唯一方法是使用 - UITableViewCellStateShowingEditControlMask,而不是你给我的一次。然而,它也伴随着延迟。不过,您的回答似乎很有意义,并且让我到目前为止。
  • willTransitionToState方法中应该是if (state == UITableViewCellStateEditingMask)。但是感谢您的回答,投票。
【解决方案2】:

其他解决方案可能会起作用,但这种方式会自动为您制作动画。 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];

【讨论】:

  • 我已经添加了您在顶部说的代码(第三批代码)是正确的地方吗?它仍然不起作用。但是在 TableView 中,我仍然按照我的方式设置文本标签??
  • 抱歉,如果您查看原始问题,我已将提交的代码分成 3 段。第三个是我指的那个。它就在问题的底部。
  • 好的,你使用 xib 的。不知道,这里是如何使用xibs或不使用xibs。 developer.apple.com/library/ios/#documentation/UserExperience/…
  • 是的,我已经经历过了,但我并不聪明:-)
  • 好的,我按照代码的方式重新创建了没有情节提要的布局。非常感谢您的帮助 - 已接受答案:-)
【解决方案3】:

这是一个 hack,但唯一对我有用的是放入一个隐藏的图像并将我的标签限制在它上面。

例如:

我在 UITableViewCell 中有一个 UILabel,当我在屏幕上和屏幕上为我的表格设置动画时,它没有随单元格移动。但其他单元格工作得很好。我什么都试过了。

我最终将图像放在标签的左侧(就像其他单元格一样),并将标签的约束添加到固定宽度的图像,并将图像左侧的约束添加到容器。

这解决了我的问题,现在标签与单元格一起动画。

我不确定标签的 jenky 布局是怎样的,但我尝试了所有的内容模式,并在设置中玩了两个多小时,但没有其他任何效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多