[英]How to change height of custom cell on button's click?
我有一個關於自定義單元格高度的問題。 單擊單元格上的一個按鈕時,我需要增加單元格的高度。 她我知道要使用兩種方法(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;
}
嘗試將布爾變量添加為類成員。 單擊按鈕時進行設置。 在- (IBAction)btnRestPlusClicked:(id)sender
的末尾重新加載表視圖
在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
,如果設置了布爾變量,則更改- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
單元格的高度。
問題不是不是heightForRowAtIndexPath:
的實現,而是要再次使運行時調用 heightForRowAtIndexPath:
以便它可以發現您的像元高度已更改。 為此,您的按鈕應該告訴表視圖reloadData
。
您可以在點擊該單元格按鈕時更改所選單元格的高度,例如
ViewController.h
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UITableView *myTableView;
}
@end
ViewController.m
@interface ViewController ()
@end
@implementation ViewController {
NSArray *tableData;
int currentselection;
int cellno;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
currentselection = -1;
tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath {
int rowHeight;
if ([indexPath row] == currentselection) {
rowHeight = 200;
} else {
rowHeight = 50;
}
return rowHeight;
}
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
NSInteger myInteger = indexPath.row;
cellno = (int) myInteger;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
// add custom expand height button
UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addFriendButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[addFriendButton setTitle:@"Add" forState:UIControlStateNormal];
[cell addSubview:addFriendButton];
[addFriendButton addTarget:self action:@selector(addHeight:) forControlEvents:UIControlEventTouchUpInside];
[addFriendButton setTag:cellno];
return cell;
}
- (IBAction) addHeight:(UIButton *)sender {
NSInteger myInteger = sender.tag;
currentselection = (int) myInteger;
[myTableView beginUpdates];
[myTableView endUpdates];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.