iWi*_*ard 4 xcode uitableview ios
我有动态tableView和一个问题.在每个tableCell中有两个按钮.我试图让按钮触及indexPath.row但没有成功.
我的代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.homeTableView indexPathForSelectedRow];
NSLog(@"%d", indexPath.row);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得indexPath.row for touced按钮(Segue连接通过这两个按钮 - >两个连接)?
Tom*_*cki 10
它不起作用,因为单击按钮时没有选择行.
使用两个按钮,我会断开你的segue与按钮,并在IB中做2个手动segue然后添加这样的代码来处理它们:
-(void)button1Pressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.homeTableView indexPathForCell:clickedCell];
...
[self performSegueWithIdentifier:@"YourManualSegueIdentifier1" sender:self];
}
Run Code Online (Sandbox Code Playgroud)
编辑第二个选项:
在MyTableViewCell.h中:
@interface MyTableViewCell : UITableViewCell
...
@property (strong, nonatomic) NSIndexPath *cellIndexPath;
...
@end
Run Code Online (Sandbox Code Playgroud)
在MyTableViewCell.m中:
-(void)button1Pressed:(id)sender {
...
NSInteger row = cellIndexPath.row;
...
}
Run Code Online (Sandbox Code Playgroud)
在MyTableViewController.m中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// remember index here!
[cell setCellIndexPath:indexPath];
....
return cell;
}
Run Code Online (Sandbox Code Playgroud)
Bho*_*opi 10
Apple在iOS 7中更改了UITableViewCell层次结构
使用iOS 6.1 SDK
<UITableViewCell>
| <UITableViewCellContentView>
| | <UILabel>
Run Code Online (Sandbox Code Playgroud)
使用iOS 7 SDK
<UITableViewCell>
| <UITableViewCellScrollView>
| | <UITableViewCellContentView>
| | | <UILabel>
Run Code Online (Sandbox Code Playgroud)
因此,如果您想在按钮索引处获取indexpath,请使用以下代码使用iOS 6或更高版本的SDK
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
NSLog(@"index=%@",clickedButtonPath);
Run Code Online (Sandbox Code Playgroud)
使用iOS 7或7+ SDK
UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
NSLog(@"index=%ld",(long)clickedButtonPath.item);
Run Code Online (Sandbox Code Playgroud)
我没有使用故事板但是一旦我在简单的基于viewconroller的项目中做到了,我认为这可能对你有帮助.
- (IBAction)goToNew:(id)sender
{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) [btn superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
int row = indexPath.row;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9390 次 |
| 最近记录: |