2012-11-24 6 views
0

단추가있는 사용자 지정 tableview 셀이 있습니다. 버튼을 클릭하면 tablview의 NSIndexPath 객체를 전달해야합니다. 나는 단추에 태그를 할당하고 발신자를 사용하여 태그를받습니다. 그러나 NSIndexPath 객체를 전달하려고합니다. 아래 코드가 있습니다.단추를 클릭 할 때 개체를 전달하는 방법

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"shortCodeCell"; 
    IPadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"KeywordsCell"  owner:nil options:nil]; 

     for (id currentObject in topLevelObjects) 
     { 
      if ([currentObject isKindOfClass:[IPadTableViewCell class]]) 
      { 
       cell = (IPadTableViewCell *)currentObject; 
      } 
     } 
    } 
    // Delete 
    [cell.btnDelete addTarget:self action:@selector(onDelete:) forControlEvents:UIControlEventTouchUpInside]; 

    cell.btnDelete.tag=indexPath.row; 

    return cell; 
} 

-(void)onDelete:(id)sender 
{ 
    UIButton *btn=(UIButton *)sender; 
    NSLog(@"BtnIndexpath to be deleted:%d",btn.tag); 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
    int errorCode = 0; 
    kd = [items objectAtIndex:btn.tag]; 
    BOOL isDeleteKeyword= [ServerAPI deleteKeywordWithId:kd.keywordId :&errorCode]; 
    dispatch_async (dispatch_get_main_queue(), 
        ^{ 
         if (isDeleteKeyword) { 
          [items removeObjectAtIndex:btn.tag]; 
          //[tblKeywords deleteRowsAtIndexPaths:[NSArray arrayWithObject:btnIndexPath] withRowAnimation:YES]; 
          [self.tblKeywords reloadData]; 
         } 
         else return; 
        }); 
    }); 
} 
+0

귀하의'tag'은'indexPath.row'에 같습니다. 'indexPath'에서 다른 어떤 정보를 원하십니까? – sunkehappy

+0

아래의 onDelete 메소드에서 indexPath.row 인 정수 값이 아닌 indexPath 객체 만 전달하려고합니다. – user578386

답변

2

당신은 "cellForRowAtIndexPath"

cell.tag = indexPath.section; 
cell.btnDelete.tag=indexPath.row; 

에 그리고 "onDelete는"당신이 할당 된 태그를 기준으로 indexPath를 만들 수에서 다음을 설정할 수 있습니다.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:[[sender superview] tag]] 
+0

괜찮아 질거야. – user578386

+0

고마워, 그걸 완전히 해냈어. 하나의 작은 덕목. 행 셀이 슬라이드 애니메이션을 삭제했다. 한 번만 애니메이션이 완성된다. tableview 셀이 있어야한다. 위에서 ..하지만 모두 같은 시간에 일어나고 있습니다. – user578386

+0

셀을 이동 하시겠습니까? –

1
// I assume your UITableView called tableView 
// Using this way you don't need to use tag 
-(void)onDelete:(id)sender 
{ 
    UITableViewCell *cell = (UITableViewCell *)[sender superview]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
} 
+0

이것은 가장 좋은 방법입니다. :-) –

+0

귀하의 답변도 작동합니다. 그러나 OP의 질문과 조금 다릅니다. 그/그녀는 그'NSIndexPath' 객체를 전달하기를 원합니다. 하지만 새로운 것을 만들면됩니다. 반대편에서 OP는'NSIndexPath'의 다른 정보를 필요로하지 않습니다. 그래서 당신의 대답도 작동합니다. – sunkehappy

관련 문제