如何获取索引路径?

时间:2012-08-22 09:26:23

标签: 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?

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

我不确定我是否得到了你的问题,但是为了将值传递给方法,你有很多可能性:

  • 声明一个实例变量,赋值,并在方法
  • 中使用它
  • 声明一个全局变量(糟糕的想法!),分配值,并在方法中使用它
  • 将其用作方法的参数(仅当方法签名未强制执行时,例如通过协议)