2013-09-25 49 views
28

Tôi đã tạo lập trình UITableView và thêm UISwitch vào chế độ xem phụ kiện ô của nó theo chương trình.IOS 7 - Cách lấy indexPath từ nút được đặt trong UITableViewCell

Đây là mã của tôi cho UISwitch trong chế độ xem phụ kiện di động theo phương pháp cellForRowAtIndexPath.

UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero]; 
[accessorySwitch setOn:NO animated:YES]; 
[accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged]; 
cell.accessoryView = accessorySwitch; 

Đây là phương pháp được gọi sau khi nút được nhấp.

- (void)changeSwitch:(UISwitch *)sender{ 

    UITableViewCell *cell = (UITableViewCell *)[sender superview]; 

    NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell]; 
    NSLog(@"%ld",(long)indexPath); 

……………. My other code……. 
} 

tôi có thể in các giá trị đường dẫn chỉ mục trong iOS 6 Nhưng trong iOS 7 nó in bằng không,

Am i thiếu bất cứ điều gì trong iOS 7 hoặc có một số phương pháp khác để có được indexPath trong iOS 7

Cảm ơn, Arun.

Trả lời

61

NSLog(@"%ld",(long)indexPath); là sai này sẽ in địa chỉ con trỏ của indexPath

thử sử dụng mã sau

CGPoint center= sender.center; 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.filterTableView]; 
    NSIndexPath *indexPath = [self.filterTableView indexPathForRowAtPoint:rootViewPoint]; 
    NSLog(@"%@",indexPath); 
+0

nó cũng đang in null – Arun

+1

bạn cũng có thể in sender.center không? và rootViewPoint – lanbo

+0

Cảm ơn bạn rất nhiều, nó đã giải quyết được vấn đề của tôi. – Arun

4

Bạn có thể gán thẻ cho mỗi chuyển đổi trong phương pháp cellForRowAtIndexPath như

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero]; 
    [accessorySwitch setOn:NO animated:YES]; 
    [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged]; 
    cell.accessoryView = accessorySwitch; 
    accessorySwitch.tag = indexPath.row;  
} 

- (void)changeSwitch:(UISwitch *)sender{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[sender tag]]; 
    NSLog(@"%ld",(long)indexPath); 
} 
+0

Nhưng tôi cần cả hai indexPath.row và indexPath.section trong mã của tôi. – Arun

1

Bạn có lẽ bị đốt cháy, làm cho giả định rằng các superview của switch sẽ là sự tableViewCell. Có lẽ trong iOS 7 họ đã thay đổi hệ thống phân cấp để đạt được một số loại hiệu ứng hình ảnh; có thể có một cái nhìn mới được chèn vào trong đó.

Bạn có thể có thể phân lớp UISwitch và cung cấp thuộc tính indexPath. Sau đó, trong số cellForRowAtIndexPath, hãy gán nó ở đó để bạn có một tham chiếu.

6

Nếu bạn có nút trong phòng giam. Bạn có thể nhận được di động bằng cách gọi superview. Và sau đó có thể lấy Indexpath bằng cách này.

(void)obButtonTap:(UIButton *)sender { 

    UITableViewCell *cell = (UITableViewCell *)sender.superview; 
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
} 
+0

Nó không hoạt động trong iOS 7 –

+0

Đây không phải là người phụ thuộc iOS 7 ... kiểm tra mã của bạn có thể là vấn đề với mã của bạn. –

+0

iOS 7 đã thay đổi cấu trúc phân cấp http://stackoverflow.com/questions/19162725/access-ios-7-hidden-uitableviewcellscrollview –

0

Tôi nghĩ rằng rất nguy hiểm khi dựa vào vị trí của nút để biết người nào đã được nhấp vào.

Tôi prefere tạo một từ điển dùng như một chìa khóa nút/chuyển đổi chính nó, và như là một giá trị indexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
... 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:[cell settingSwitch]]; 
    [[self switchIndexDictionary]setObject:indexPath accessorySwitchKey]; 
... 
} 

sau đó khi chuyển đổi/nút được kích hoạt tôi nhận được indexPath dễ dàng từ điển của tôi:

- (void)toggleSetting:(id)sender 
{ 
    UISwitch *selectedSwitch = (UISwitch *) sender; 

    NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:selectedSwitch]; 
    NSIndexPath *indexPath = [[self switchIndexDictionary]objectForKey: accessorySwitchKey]; 
} 
5

Swift giải pháp: một UITableView mở rộng như thế này có thể hữu ích cho việc này.

extension UITableView { 
    func indexPathForView(view: AnyObject) -> NSIndexPath? { 
     let originInTableView = self.convertPoint(CGPointZero, fromView: (view as! UIView)) 
     return self.indexPathForRowAtPoint(originInTableView) 
    } 
} 

Sử dụng trở nên dễ dàng ở mọi nơi.

let indexPath = tableView.indexPathForView(button) 
0

SWIFT 4:

extension UITableView { 
    func indexPathForView(view: AnyObject) -> NSIndexPath? { 
     let originInTableView = self.convert(CGPoint.zero, from: (view as! UIView)) 
     return self.indexPathForRow(at: originInTableView)! as NSIndexPath 
    } 
}