如何在PrepareForSegue方法-iOS中获取自定义单元格的indexPath
我有一个自定义的如何在PrepareForSegue方法-iOS中获取自定义单元格的indexPath,ios,objective-c,uitableview,segue,nsindexpath,Ios,Objective C,Uitableview,Segue,Nsindexpath,我有一个自定义的单元格,里面几乎没有视图和按钮。从其中一个按钮,我正在执行一个Segue,我需要那个单元格号码,但不知道如何获取它。请帮忙 下面是我手机的外观: 从红色圆圈按钮调用prepareforsgue 代码如下: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _array.count; } - (NSInteger)tableView:(UITableView *)tabl
单元格
,里面几乎没有视图和按钮。从其中一个按钮,我正在执行一个Segue
,我需要那个单元格号码,但不知道如何获取它。请帮忙
下面是我手机的外观:
从红色圆圈按钮调用prepareforsgue
代码如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
if (cell) {
cell.item = _array[indexPath.section];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"AudioComment"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSLog(@"%ld",(long)indexPath.section); //printing 0 everytime :(
}
}
请提供有关如何获取indepath
的任何帮助/建议
谢谢。您可以使用
UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(@"%ld",(long)[_generalTableView indexPathForCell:cell].row);
你可以用
UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(@"%ld",(long)[_generalTableView indexPathForCell:cell].row);
为索引添加一个变量,并在选择单元格时进行设置。访问perpareForSegue中的变量,将其传递给destinationVC并将其置为0。为索引添加一个变量,并在选择单元格时进行设置。访问perpareForSegue中的变量,将其传递给destinationVC并将其置为0。使用下面的代码。这可能对你有帮助
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MainPhotoCell *cell = (MainPhotoCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifierMedia" forIndexPath:indexPath];
if (!cell)
{
cell = [[MainPhotoCell alloc] init];
}
cell.usrCommentBtn.tag = indexPath.row;
[cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)onCommentClick:(id)sender
{
if ([GlobalManager checkUserLoginStatus:self])
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([sender tag]) inSection:0];
[self performSegueWithIdentifier:@"showComments" sender:indexPath];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: @"showComments"])
{
NSIndexPath * path = (NSIndexPath *)sender;
//do the your required code here.
}
}
使用下面的代码。这可能对你有帮助
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MainPhotoCell *cell = (MainPhotoCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifierMedia" forIndexPath:indexPath];
if (!cell)
{
cell = [[MainPhotoCell alloc] init];
}
cell.usrCommentBtn.tag = indexPath.row;
[cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)onCommentClick:(id)sender
{
if ([GlobalManager checkUserLoginStatus:self])
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([sender tag]) inSection:0];
[self performSegueWithIdentifier:@"showComments" sender:indexPath];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: @"showComments"])
{
NSIndexPath * path = (NSIndexPath *)sender;
//do the your required code here.
}
}
你必须在按钮上设置标签。在您的情况下,在CellForRowatineXpath中将按钮标记设置为indexPath.section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
if (cell) {
cell.item = _array[indexPath.section];
}
cell.button1.tag = indexPath.section;
return cell;
}
在PrepareForSegue中,从按钮标记获取索引
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"AudioComment"])
{
NSInteger index = ((UIButton *) sender).tag;
NSLog(@" %ld ",index);
}
}
你必须在按钮上设置标签。在您的情况下,在CellForRowatineXpath中将按钮标记设置为indexPath.section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
if (cell) {
cell.item = _array[indexPath.section];
}
cell.button1.tag = indexPath.section;
return cell;
}
在PrepareForSegue中,从按钮标记获取索引
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"AudioComment"])
{
NSInteger index = ((UIButton *) sender).tag;
NSLog(@" %ld ",index);
}
}
这将不起作用,因为如果您单击表格单元格中的UIButton,该单元格将不会被选中。
sender.superview.superview
行给出错误:“在对象类型'\u strong id''上找不到属性'superview'”@SHA您可以再试一次吗?@Misha刚刚尝试了更新的代码,在每个单元格的按钮点击时仍为0。您可以记录单元格及其内容吗?我这边很好。是否要用UITableViewCell替换单元格?按钮在每个单元格中?这不起作用,因为如果您单击表单元格中的UIButton,该单元格将不会被选中。sender.superview.superview
行给出错误:“在对象类型'\u strong id''上找不到属性'superview'”@SHA您可以再试一次吗?@Misha刚刚尝试了更新的代码,在每个单元格的按钮点击时仍为0。您可以记录单元格及其内容吗?我这边很好。是否要用UITableViewCell替换单元格?按钮在每个单元格中?不起作用:(onCommentClick
从未被调用,直接调用prepareforsgue
。不要这样做“[cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:)forControlEvents:uicontroleventTouchInside];”-将原型单元格与iActions和委派一起使用,从单元格到VCSure,任何代码都可以工作,但并不意味着它是好的。这种方式违反了OO设计原则。@Helium3。谢谢你的建议。我下次会提醒它。@Hardikshek我需要将segue
也从故事板中的按钮断开吗?不工作:(onCommentClick
永远不会被调用,直接调用prepareforsgue
。不要这样做“[cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:)forControlEvents:UIControlEventTouchUpInside];”-将原型单元格与IBActions和委派一起使用,从单元格到VCSure,任何代码都可以工作,但并不意味着它好。这种方式违反了OO设计原则。@Helium3。谢谢你的建议。我下次会提醒你。@Hardikshek我还需要将segue
从故事板中的按钮断开吗?我没有选择cell,asdidselectrowatinex
从未被调用,因为我点击了该单元格中的特定按钮。如何获取索引我没有选择单元格,asdidselectrowatinex
从未被调用,因为我点击了该单元格中的特定按钮。我如何获得比Hardik Shekat更好的indexA解决方案:)谢谢:)一个比Hardik Shekat更好的解决方案:)谢谢:)UITableView的indexPathForSelectedRow
会不会不起作用?(文档)UITableView的indexPathForSelectedRow
会不会不起作用?(文件)