[英]iOS - TableView - Get Index of Header section on tapping button
我已經為UITableView實現了自定義標題部分。 在Header的自定義視圖中有一個按鈕,我希望獲得該部分的索引以點擊該按鈕的操作。
我可以嘗試一下去
CustomHeader *view = (CustomHeader*)btnObject.superView;
但是從這個headerView的實例中我怎樣才能獲得該部分的IndexPath或索引?
編輯:我需要一些獨立於標簽的東西或任何類似於Cell的索引路徑的東西,如下所示
-(NSIndexPath*)indexPathFromSenderView:(UIView*)view{
CGPoint center= view.center;
CGPoint rootViewPoint = [view.superview convertPoint:center toView:self.tblMainContainer];
NSIndexPath *indexPath = [self.tblMainContainer indexPathForRowAtPoint:rootViewPoint];
return indexPath;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// create custom header here
// set up a button method
[yourButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// set the button tag as section
yourButton.tag = section;
return yourCustomHeaderWithButton;
}
later
-(void)buttonAction:(id)sender{
UIButton *clickedButton = (UIButton*)sender;
NSLog(@"section : %i",clickedButton.tag);
}
您需要使用標簽。 與普通單元格不同,無法從標題部分獲取indexPath。 如果需要,可以將標題部分視圖改為UITableViewCell行,而只需將其添加為部分的頂行。
BTW你可以使用
[self.tableView indexPathForCell:cell]
而是獲取單元格的索引路徑。
如果您決定使用標記,則可以使用操作方法中的以下代碼更新標記值:
[UIView animateWithDuration:0.2 animations:^{
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionToDelete] withRowAnimation:UITableViewRowAnimationAutomatic];
} completion:^(BOOL finished){
[self.tableView reloadData];
}];
這樣可以防止標記值出錯並阻止應用程序崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.