簡體   English   中英

如果單元格有按鈕,我們如何獲得單元格的行號和節號

[英]How can we get row and section no of cell if cell is having button

我已經摸索了表格視圖,每個部分我都有兩行,其中的行我有多個按鈕,假設我已經在第一個單元格按鈕上單擊了第0部分,我如何才能獲得該單元格的部分號和行號。

這是我的cellforrow,我正在獲取第no行,但如何找出第no行?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [[cell contentView] setBackgroundColor:[UIColor clearColor]];
        [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
         [cell.Mybutton addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];
    }
     cell.Mybutton.tag=indexPath.row;
}

-(void)btnCommentClick:(id)sender
{
        UIButton *senderButton = (UIButton *)sender;  
        NSLog(@"current Row=%d",senderButton.tag);
        NSIndexPath *path = [NSIndexPath indexPathForRow:senderButton.tag inSection:0];
}

嘗試這個:

- (void) btnCommentClick:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ...
    }
}

您可以從indexPath.section中的部分

正確處理此問題的一種方法如下。 注意,如果您認為這是很多多余的工作,那么您要么很懶惰,還是非常糟糕的設計的擁護者,或者您沒有獲得如何設置流暢設計的OO代碼的方法。

  • 創建一個自定義的UITableViewCell子類
  • 將按鈕的目標設置為單元格
  • 在子類UITableVIewCell的.h文件中,在tableViewCell上創建一個協議和一個弱屬性,該屬性和該協議一致,例如:

     @class MyTableViewCell @protocol MyTableViewCellDelegate - (void) leftButtonTappedOnTableViewCell: (MyTableViewCell *) cell; - (void) rightButtonTappedOnTableViewCell: (MyTableViewCell *) cell; @end @interface MyTableViewCell: UITableViewCell @property (nonatomic, weak) id< MyTableViewCellDelegate> delegate; @end 
  • 確保在tableViewCell中的選擇器響應於按鈕而被調用,從而在單元的委托屬性上調用委托方法之一。

  • 回到viewController的cellForRowAtIndexPath中,應確保使用UITableVIewCell子類,並將委托設置為UIViewController。 然后,實現這兩個委托方法,您可以通過向tableVIew索取該單元格的indexPath:

     - (void) rightButtonTappedOnTableViewCell: (MyTableViewCell *) cell{ NSIndexPath * indexPath = [self.tableView indexPathForCell: cell]; // ... do something now that you now the indexPath } 

tableViewCell是視圖層次結構中某處的超級視圖。 您可以遍歷超級視圖以找到tableViewCell。

- (UITableViewCell *)cellForButton:(UIView *)button {
    if ([button.superview isKindOfClass:[UITableViewCell class]])
        return button.superview; // Return found cell
    else if (button.superview)
        return [self cellForButton:button.superview]; // Find next superview
    else
        return nil; // No cell found
}

找到后,可以通過以下方式獲取indexPath:

UITableViewCell *cell = [self cellForButton:yourButton];
IndexPath *indexPath = [tableView indexPathForCell:cell];

您可以嘗試通過為單元格和其他屬性(如以下部分的accessibilityIdentifier提供標記值。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [[cell contentView] setBackgroundColor:[UIColor clearColor]];
    [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
     cell.Mybutton.tag = indexPath.row;
     cell.Mybutton.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section];
     [cell.Mybutton addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];
       }

}

-(void)btnCommentClick:(id)sender
{
    UIButton *senderButton = (UIButton *)sender;  
    NSLog(@"current Row=%d current section =%d",senderButton.tag,[senderButton.accessibilityIdentifier intValue]);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM