2012-08-22 51 views
0

我试图找到的UITableViewCell的IndexPath当我点击一个按钮,它doesn't看起来很容易,因为我用的也是UIAlert。这我的代码我使用:如何获得indexpath?

// 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?

+0

什么是你'的tableView:didSelectRowAtIndexPath方法:'方法?你可以从那里获得indexpath.row。你能否显示该代码? :) –

回答

3

,您可以给UIButton的一个标签,并与indexPath.row标记,然后当你按下按钮距离(ID)发送对象检索,然后从那里获取的标签。

+0

谢谢,Tag对我很好 – Ben

0

我不知道如果我得到你的proplem,但对于一个值传递给一个方法,你有多种可能性:

  • 声明一个实例变量,赋值,并用它在该方法
  • 声明一个全局变量(坏主意!),赋值,并使用它的方法
  • 使用它作为您的方法的参数(只能在方法签名不是强制的,如由协议)
相关问题