问题
This is doing my head in:-)
I have a fully functional CoreData Populated UITableView
inside a UIViewController
and I have successfully implemented the "Swipe to Delete option" (which is easy) and I can also delete single instances with an edit button where the red circle thingy comes up.
My problem is, and I think it is because I have a CustomCell, that when I press the edit button the UILabels
do not move to the right.
I have tried using -(void)layoutSubViews
and a few others, but nothing works.
I have posted my code for my cellForRowAtIndexPath
. This is part of a note section in my app. This code works, I just need to know How to move the labels when I go into Edit mode??
Thank you for the tips and advice:-)
- (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;
}
回答1:
The other solution will probably work but this way will do the animation automatically for you.
MNCustomCell
is not going to re-layout the view depending on the current state of the cell, but if you add your label to the contentView of the cell, it will.
The following example will move the label so it doesn't interfere with the delete button.
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];
回答2:
Implement the following methods in your custom cell class.
- (void)willTransitionToState:(UITableViewCellStateMask)state
and
- (void)didTransitionToState:(UITableViewCellStateMask)state
and move your label accordingly.
It should be like
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
label.frame = ...
}
}
Edit:
- (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];
}
}
回答3:
Its a hack but the only thing that worked for me was putting in a hidden image and constraining my labels against it.
For instance:
I had a UILabel within a UITableViewCell that wasn't moving with the cell when I animated my table on and off the screen.. but other cells worked just fine. I tried everything.
I ended up putting an image on the left side of the label (like the other cells had), and adding constraints from the label to the fixed width image, and a constraint from the left of the image to the container.
This fixed my issue, and now the label animates with the cell.
I'm not sure what jenky layout is going on with labels, but I tried all of the content modes, and played with settings for well over two hours and nothing else worked.
来源:https://stackoverflow.com/questions/13666185/custom-uitableviewcell-in-edit-mode-does-not-move-my-uilabels