ios - 如何获取索引路径?

标签 ios uitableview

当我单击一个按钮时,我试图找到 UITableViewCell 的 IndexPath,这看起来并不容易,因为我还使用了 UIAlert。这是我正在使用的代码:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";


    UpdatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UpdatesTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (UpdatesTableViewCell*)view;
            }
        }
    }


    cell.textLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"Title"];
    cell.detailTextLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"Description"];

    //////////This button is the one sending the action////////////////
    [cell.deletebutton addTarget:self action:@selector(deletetherow:) forControlEvents:UIControlEventTouchUpInside];


    imagepath = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"Photo"];

    if([imagepath isEqualToString:@"(null)"])
    {
        cell.imageView.image = [UIImage imageNamed:@"noimage.png"];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds = YES; 

    }
    else {

        cell.imageView.image = [[[UIImage alloc] initWithContentsOfFile:imagepath] autorelease];

    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (void)deletetherow:(id)sender{

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Title"
                                                  message:@"Delete it ?"
                                                 delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"delete", nil];
[message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

if([title isEqualToString:@"Eliminar"])
{

        NSLog(@"OFFLINE");
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        
        NSString *documentsDirectory = [paths objectAtIndex:0];        
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"datatesting.plist"];
        libraryContent = [[NSMutableArray alloc] initWithContentsOfFile:path];
        [libraryContent removeObjectAtIndex:0]; ///// Here instead 0, I want to put the indexpath to know what row content to delete.
        NSLog(@"delete the Row:%@",libraryContent);
        [libraryContent writeToFile:path atomically:YES];

        dao = [[Dfetch alloc] initWithLibraryName:@"Diario"];
        [self.tableviewnew reloadData];

    }
  }
}

所以我的问题是,如何在 alertView 方法中获取 indexpath.row?

最佳答案

您可以给 UIButton 一个标签并用 indexPath.row 标记它,然后当您按下按钮时,只需从 (id)sender 对象中检索它,然后从那里检索标签。

关于ios - 如何获取索引路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12070078/

相关文章:

ios - 为什么 UITableView 总是可以滚动而 UIScrollView 不能

ios - 移动应用程序数据管理

iOS 的 UITableView 有问题以编程方式选择多个单元格并显示支票

iOS:在多任务处理中检测 Regular 或 Compact

ios - 包含 TableView 的 UIViewController 中的 UISearchBar 问题

ios - 当应用程序未在 ios 7 中运行时如何更新应用程序角标(Badge)编号?

iphone - 键盘附件 View 隐藏文本

ios - 在 UITableViewCell 中使用 AlamofireImage

objective-c - Objective C中的简单字符串连接

ios - 发送对象和发送对象指针的区别