我正在尝试在选中时创建一个动画单元格。一切似乎都很奇怪。如果我将ButtonView发送到UITableView Cell的Self.contentView,它就像它应该的那样工作并在所需的行中添加单元格,但是我在索引行处松开了按钮功能。如果我将buttonview发送到自己的“UITableViewCell”我有按钮索引行,但它大约每10行添加按钮视图。
现在,当它正常工作时,动画本身看起来很棒。问题是我正在使用一种方法切换选定的路径,这样我可以在选择时将单元格高度增大到两倍,并保持选中状态直到选择另一个单元格。
我真的不知道从哪里开始我希望也许有人可以帮助我找到我的错误或者用类似的东西进行交换。
谢谢,我感谢任何见解!
- (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;
}
来自我的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;
}
答案 0 :(得分:3)
创建单元格时,将buttonView添加到单元格的contentView
。通过设置hidden
或alpha
属性来显示和隐藏它。现在,没有让按钮工作的问题应该消失。
此外,如果您只选择了一个单元格,那么包含所选索引的词典似乎有点过头了。只需在控制器中创建类型为NSIndexPath
的“已选中”属性。
最后,滚动时重复选择的单元格是由于UITableView
的重新排队机制。确保每次都为hidden
中的按钮设置正确的cellForRowAtIndexPath
属性。这样,当具有非隐藏按钮的单元格被回收时,如果索引路径不是所选索引路径,则它将隐藏它。