如何在 PrepareForSegue 方法中获取自定义单元格的 indexPath - iOS
How to get indexPath of a custom cell in PrepareForSegue method - iOS
我有一个自定义 cell
,其中的视图和按钮很少。从其中一个按钮,我正在执行 Segue
并且我需要该手机号码但不知道如何获得它。请帮忙。
这是我的手机的样子:
prepareForSegue
从红色圆圈按钮调用。
代码如下:
- (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 :(
}
}
请提供 help/suggestions 我怎样才能得到 indexPath
?
谢谢。
您可以使用
UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(@"%ld",(long)[_generalTableView indexPathForCell:cell].row);
为索引添加一个变量,并在选择单元格时设置它。访问 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.
}
}
您必须在按钮上设置标签。在您的情况下,在 CellforRowAtIndexPath.
中将按钮标记设置为 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);
}
}
我有一个自定义 cell
,其中的视图和按钮很少。从其中一个按钮,我正在执行 Segue
并且我需要该手机号码但不知道如何获得它。请帮忙。
这是我的手机的样子:
prepareForSegue
从红色圆圈按钮调用。
代码如下:
- (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 :(
}
}
请提供 help/suggestions 我怎样才能得到 indexPath
?
谢谢。
您可以使用
UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(@"%ld",(long)[_generalTableView indexPathForCell:cell].row);
为索引添加一个变量,并在选择单元格时设置它。访问 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.
}
}
您必须在按钮上设置标签。在您的情况下,在 CellforRowAtIndexPath.
中将按钮标记设置为 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);
}
}