[英]How to change height of custom cell on button's click?
I have an issue regarding height of custom cell. 我有一个关于自定义单元格高度的问题。 I need to increase the height of cell when one button on cell was clicked.
单击单元格上的一个按钮时,我需要增加单元格的高度。 her I know to use two methods (heightforrow and didselectrow) for it but I am confuse that when I clicked on button that time custom method for button action is called and I am using those two methods in controller.I attached my code:
她我知道要使用两种方法(heightforrow和didselectrow),但我感到困惑,当我单击按钮时,调用按钮动作的时间自定义方法被调用,并且我在控制器中使用了这两种方法。我附上了我的代码:
in customcell.m 在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;
}
now I need that when button clicked that time only that row's height will increase. 现在,我需要单击该按钮时,只有该行的高度会增加。
- (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?
}
Please can any one help me out from this? 请问有人可以帮我吗? Thanks
谢谢
Create NSMutableDictionary inside of UIViewController where you will store NSIndexPath of btnRestPlusClicked cells. 在UIViewController内部创建NSMutableDictionary,您将在其中存储btnRestPlusClicked单元的NSIndexPath。
Then track when button plus selected in each cell: 然后跟踪在每个单元格中何时选择了plus按钮:
In CustomCell.h 在CustomCell.h中
@protocol CustomCellDelegate;
@interface CustomCell : UITableViewCell
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
@end
@protocol CustomCellDelegate <NSObject>
- (void)customCellButtonPlusSelected:(CustomCell*)cell;
@end
In CustomCell.m 在CustomCell.m中
- (IBAction)btnRestPlusClicked:(id)sender
{
[self.delegate customCellButtonPlusSelected:self];
}
In UIViewController when you create cell for cellForRowAtIndexPath add: 在UIViewController中,为cellForRowAtIndexPath创建单元格时,添加:
cell.delegate = self
and conform UIViewController to CustomCellDelegate 并使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;
}
Try adding a boolean variable as class member. 尝试将布尔变量添加为类成员。 Set it when you click the button.
单击按钮时进行设置。 Reload the table view at the end of
- (IBAction)btnRestPlusClicked:(id)sender
在
- (IBAction)btnRestPlusClicked:(id)sender
的末尾重新加载表视图
In - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
change the height of tableview cell if boolean variable is set. 在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
,如果设置了布尔变量,则更改- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
单元格的高度。
The problem is not the implementation of heightForRowAtIndexPath:
but in getting the runtime to call heightForRowAtIndexPath:
again, so that it can discover that your cell height has changed. 问题不是不是
heightForRowAtIndexPath:
的实现,而是要再次使运行时调用 heightForRowAtIndexPath:
以便它可以发现您的像元高度已更改。 To do that, your button should tell the table view to reloadData
. 为此,您的按钮应该告诉表视图
reloadData
。
You can change the height of your selected cell when you tap on that cell button like 您可以在点击该单元格按钮时更改所选单元格的高度,例如
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.