编辑模式下的自定义UITableViewCell不会移动我的UILabel
这是我的头脑:-)
我在UIViewController
有一个function齐全的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; }
另一种解决方案可能会起作用,但这种方式会自动为您完成动画。 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];
在自定义单元类中实现以下方法。
- (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]; } }