我有一个关于自定义单元格高度的问题。单击单元格上的一个按钮时,我需要增加单元格的高度。她知道可以使用两种方法(heightforrow和didselectrow),但是我感到困惑,当我单击按钮时,调用了按钮动作的自定义方法,并且我在控制器中使用了这两种方法。我附上了我的代码:
在customcell.m中
- (IBAction)btnRestPlusClicked:(id)sender
{
UIButton *btn = (id)sender;
btn.selected =!btn.selected;
if (btn.selected)
{
NSLog(@"selected");
// _viewExtraScheduleAmount.hidden = FALSE;//I need to make this event on button action and same time increase the height of cell.
}
else
{
btn.tag = 0;
// _viewExtraScheduleAmount.hidden = TRUE;
}
现在我需要的是,当单击该按钮时,只有该行的高度会增加。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//if (ceel.btnRestPlusClicked.selected)
//{
// return 100;
// }
// return 60;
I know I am wrong here but how to use this method?
}
请有人可以帮助我吗?谢谢
在UIViewController内部创建NSMutableDictionary,您将在其中存储btnRestPlusClicked单元的NSIndexPath。
然后跟踪在每个单元格中何时选择了plus按钮:
在CustomCell.h中
@protocol CustomCellDelegate;
@interface CustomCell : UITableViewCell
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
@end
@protocol CustomCellDelegate <NSObject>
- (void)customCellButtonPlusSelected:(CustomCell*)cell;
@end
在CustomCell.m中
- (IBAction)btnRestPlusClicked:(id)sender
{
[self.delegate customCellButtonPlusSelected:self];
}
在UIViewController中,为cellForRowAtIndexPath创建单元格时,添加:
cell.delegate = self
并使UIViewController符合CustomCellDelegate
-(void)customCellButtonPlusSelected:(CustomCell*)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSString *key = [NSString stringWithFormat:@"%li:%li", indexPath.section, indexPath.row];
[buttonPlusClickedDictionary setObject:@"1" forKey:key];
[self.tableView reloadRowsAtIndexPaths:@[indexPath]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [NSString stringWithFormat:@"%li:%li", indexPath.section, indexPath.row];
if ([buttonPlusClickedDictionary objectForKey:key]) {
return 100;
}
return 60;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句