2012-08-22 86 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,但對於一個值傳遞給一個方法,你有多種可能性:

  • 聲明一個實例變量,賦值,並用它在該方法
  • 聲明一個全局變量(壞主意!),賦值,並使用它的方法
  • 使用它作爲您的方法的參數(只能在方法簽名不是強制的,如由協議)
相關問題