I am attempting to create a animated cell when it is selected. Everything seems to be working really odd. If I send the ButtonView to the Self.contentView of the UITableView Cell it works like it is supposed to and adds the cell in desired row but I loose button functionality at index row. If I send the buttonview to just self "UITableViewCell" I have button index row but it adds the button view in about every 10 rows.
Now the animation itself looks great when it works right. The issue is I am using a method to toggle the path selected so I can grow the cell height to twice the size when selected and stay selected until another cell is selected.
Really I have no idea whe开发者_StackOverflowre to go from here I was hoping maybe someone could help me find my mistake or have delt with something similar.
Thanks I appreciate any insight!
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
[sounds PlayButtonClick:nil];
// Deselect cell ..?
//[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
// This is where magic happens...
[tableView beginUpdates];
[tableView endUpdates];
[sounds PlaySlide:nil];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath {
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return 70 *2;
}
return 70;
}
And from my CellClass
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self == [super initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reuseIdentifier]) {
self.contentView.clipsToBounds = YES;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected == YES) {
NSLog(@"is selected");
[self showButtonView:!selected];
[self applyDropShadow:!selected];
// TweettextLabel.hidden = YES;
// Tweettext.hidden = NO;
}
if (selected == NO){
[self removeButtonView:!selected];
[self removeDropShadow:!selected];
//TweettextLabel.hidden = NO;
//Tweettext.hidden = YES;
NSLog(@"is NOT selected");
}
}
-(void)showButtonView:(int)view{
CGRect viewFrame = [ButtonView frame];
viewFrame.origin.y = 0;
self.ButtonView.frame = viewFrame;
[self.contentView insertSubview:self.ButtonView belowSubview:self.cellView];
[self addSubview:self.contentView];
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:0.3];
CGRect viewFrame1 = [ButtonView frame];
viewFrame1.origin.y = 70;
ButtonView.frame = viewFrame1;
[UIView commitAnimations];
}
-(void)removeButtonView:(int)view{
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:0.3];
CGRect viewFrame1 = [self.ButtonView frame];
viewFrame1.origin.y = 0;
self.ButtonView.frame = viewFrame1;
[UIView commitAnimations];
// timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self
selector:@selector(removeLastViewFromSuper) userInfo:Nil repeats:NO];
}
-(void)applyDropShadow:(int)shadow{
self.contentView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.contentView.layer.shadowOffset = CGSizeMake(0.8f, -4.0f);
self.contentView.layer.shadowRadius = 5.0f;
self.contentView.layer.shadowOpacity = 1.0f;
}
-(void)removeDropShadow:(int)shadow{
self.contentView.layer.shadowColor = [[UIColor clearColor] CGColor];
self.contentView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.contentView.layer.shadowRadius = 0.0;
self.contentView.layer.shadowOpacity = 0.0;
}
Add the buttonView to the cell's contentView
when you create it. Show and hide it by setting the hidden
or alpha
property. Now the problem of not getting the button to work should go away.
Also, your dictionary with the selected indexes seems a bit overdone if you only have one cell that is selected. Just create a property "selected" of type NSIndexPath
in the controller.
Finally, your selected cell repeating when you scroll is due to the re-queuing mechanism of UITableView
. Make sure that you set the correct hidden
property for the button in cellForRowAtIndexPath
each time. This way, when a cell is recycled that has a non-hidden button, it will hide it if the index path is not the selected one.
精彩评论