我有一个UITableViewCell与UISwitch作为accessoryView的每一个细胞.当我更改单元格中交换机的值时,如何知道交换机在哪一行?我需要切换值更改事件中的行号.
我以编程方式创建了一个UITableView,并将UISwitch添加到它的单元附件视图中.
这是我在cellForRowAtIndexPath方法中的单元附件视图中的UISwitch代码.
UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
[accessorySwitch setOn:NO animated:YES];
[accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = accessorySwitch;
Run Code Online (Sandbox Code Playgroud)
这是单击按钮后调用的方法.
- (void)changeSwitch:(UISwitch *)sender{
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];
NSLog(@"%ld",(long)indexPath);
……………. My other code…….
}
Run Code Online (Sandbox Code Playgroud)
我能够在iOS 6中打印索引路径值但是在iOS 7中它打印为nil,
我在iOS 7中遗漏了什么,或者在iOS 7中有其他方法来获取indexPath
谢谢,阿伦.
我有一个UITableView使用原型单元格.
细胞具有UIButton显示UIViewController(模态)使用的细胞Segue.
我的问题是:我如何将IndexPath单元格传递给第二个视图控制器
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showGamerDetail"]) {
//its not worked:
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
nDetailViewController *destinationViewController = segue.destinationViewController;
destinationViewController.selectedRowTitle = [gamers objectAtIndex:indexPath.row];
destinationViewController.indexPath = indexPath;
destinationViewController.delegate = self;
}
Run Code Online (Sandbox Code Playgroud) 如何获取我在 Xcode 中使用 Swift for iOS 制作的 CollectionView 中单击的“Sheep”的索引?
class SheepsOverviewVC:
UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "class", for: indexPath) as! ClassesCollectionCell
if(sheeps.count > 0) {
cell.ClassImageView.image = UIImage(named: sheeps[indexPath.row] as! String)
cell.SheepName.text = names[indexPath.row] as? String
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
我通过 Gui 在 TouchDown 上创建了一个发送事件:
@IBAction func clickingSheep(_ sender: UIButton) {
print("This will show info about the Sheep")
print(sender)
}
Run Code Online (Sandbox Code Playgroud)
但我得到的回应来自第二次印刷:
<UIButton: 0x7f9a63021d20; frame = (50 50; …