[英]How to determine a Button click in a Custom UITableViewCell?
我有一個帶有自定義單元格和節標題的TableView。 標頭向我顯示國家,自定義單元格向我顯示城市。
看起來像這樣:
美國
紐約
洛杉磯
德國
柏林
法蘭克福
每個單元都有一個按鈕。 按下后,它將導航並將城市名稱推送到詳細信息視圖。
問題是,按下按鈕后,我不知道如何確定城市名稱。
我有一個解決方案,但是它不再起作用了:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
StandortCell *cell = (StandortCell *)[tableView dequeueReusableCellWithIdentifier:@"ortCell"];
[cell.detailButton setTag:indexPath.row];
cell.ortLabel.text = [managedObject valueForKey:@"stadtname"];
}
- (IBAction)detailButton:(id)sender {
UIView *contentView = [sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
NSInteger section = cellIndexPath.section;
UIButton * myButton = sender;
int row=myButton.tag;
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];
StrasseViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"strasse"];
self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];
detail.currentStandort = self.selectedStandort;
[self.navigationController pushViewController:detail animated:YES];
}
當我使用舊代碼時,它只會在第一部分中顯示正確的街道行。 它沒有檢測到其他部分(國家);
當我選擇第三部分的第一個城市時。 它會在第一部分向我顯示第一個城市。
我希望你能幫助我。
使用它,希望它能解決您的目的。
在視圖控制器中創建按鈕按下的方法
(IBAction)detailButton:(UIButton *)發送者{
CustomUITableViewCellName * cell =(CustomUITableViewCellName *)[[sender superview] superview]; NSIndexPath * cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell];
}
對於ios 7,添加另一個超級視圖。
現在,通過這種方式,您將獲得IndexPath,並通過使用indexPath.row或indexPath.section獲得當前行。
您可以使用sender.title屬性獲得標題。
您可以在數組中使用此indexPath來獲取所需的詳細信息。
您需要分別發送行和節信息,因此請標記為
標簽=行* 1000 +部分。
現在從標簽獲取使用行=標簽/ 1000部分=標簽%1000的節和行值
使用此值可以制定正確的索引路徑,或者找到2分鍾左右更好的解決方案來獲取該值。
警告其解決方法正確的是
在CustomButton.h中
@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end
在CustomButton.m中
@implementation CustomButton
@synthesize path;
@end
在表格視圖單元格上使用此自定義按鈕,而不是使用標記集路徑屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.