簡體   English   中英

如何在單擊按鈕上獲取UITableviewcell(自定義單元格)值

[英]How to get UITableviewcell(custom cell) values on click on button

我有一個UITableViewCell (自定義單元格),我在其中創建一些按鈕和文本字段,並為按鈕和文本字段分配標簽。 但是我無法在點擊按鈕上獲得按鈕標題和文本字段值。

cellForRowAtIndexPath

`[((CustomCell *) cell).btn setTag:rowTag];
 [((CustomCell *) cell).textField2 setTag:rowTag+1];`

-(IBAction)submitBtnAction:(UIControl *)sender
{
    for (int i=0; i<[self->_dataArray count]; i++)
    {
        NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0];
        NSLog(@"myIP.row %d",myIP.row);
        UITableViewCell *cell = [tblView1 cellForRowAtIndexPath:myIP];
        NSLog(@"tag %d",cell.tag);
        UIButton *btn = (UIButton *)[cell.contentView viewWithTag:i];
        NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag);
        UITextField *tf = (UITextField *)[cell.contentView viewWithTag:i+1];
        NSLog(@"tf text %@, tag %d",tf.text,btn.tag);

    }
}

我收到這樣的錯誤

-[UITableViewCellContentView titleLabel]: unrecognized selector sent to instance 0x71844e0
2013-07-17 13:48:29.998 Text[1271:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView titleLabel]: unrecognized selector sent to instance 0x71844e0'

我認為,一旦從cellForRowAtIndexPath:獲取它,就可以直接訪問單元格的btntextField2屬性cellForRowAtIndexPath: 假設您正在創建並返回CustomCell實例,只需將其強制轉換為CustomCell而不是UITableviewCell 見下面的修改代碼

-(IBAction)submitBtnAction:(UIControl *)sender
{
        UIButton *button = (UIButton*)sender;
        NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0];
        //Type cast it to CustomCell
        CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:myIP];
        UIButton *btn = cell.btn;
        NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag);

        UITextField *tf = cell.textField2;
        NSLog(@"tf text %@, tag %d",tf.text,btn.tag);
}

希望有所幫助!

簡單的方法:

-(IBAction)submitBtnAction:(UIControl *)sender
{
   UIButton *senderButton = (UIButton *)sender;

    NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0];

    CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:myIP];

    NSLog(@"cell.textField -tag :%d",cell.textField2.tag);

    NSLog(@"cell.btn -tag :%d",cell.btn.tag);

}

以下代碼通過“event”參數獲取indexPath可能更好:

-(IBAction)submitBtnAction:(UIControl *)sender event:(id)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPos = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView  indexPathForRowAtPoint:touchPos];
    if(indexPath != nil)
    {
       //Todo: get model at indexPath, update cell or something other 
    }
}

submitBtn的目標選擇器確認更改為@selector(submitBtnAction:event:)

通常當您收到unrecognized selector錯誤時,這是​​因為您正在訪問已在內存中替換的對象。 在您的情況下,您可能正在訪問不可見的Cell,因此contentview返回nil

當你進行for循環時,你似乎訪問了所有不可見的細胞。 對我來說,你只能訪問可見的單元格orelse contentview是nil,因此訪問titleLabel會給你無法識別的選擇器錯誤。

在標記值中添加填充。 否則,第一行標記為0且與內容視圖標記匹配,默認情況下所有視圖標記均為0。 因此,當tag等於0時,為什么會得到錯誤的視圖。

#define PADDING 100
[((CustomCell *) cell).btn setTag:PADDING + rowTag];
[((CustomCell *) cell).textField2 setTag:PADDING + rowTag+1];

但是,我會將解決方案更改為不使用增量標記而是使用靜態標記。 您已經通過cellForRowAtIndexPath:獲得了特定的單元格cellForRowAtIndexPath:您只需要該單元格的按鈕即可。

#define BUTTON_TAG 10
#define TEXT_TAG 11

cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"] autorelease];
    [((CustomCell *) cell).btn setTag:BUTTON_TAG];
    [((CustomCell *) cell).textField2 setTag:TEXT_TAG];
}

-(IBAction)submitBtnAction:(UIControl *)sender
{
    for (int i=0; i<[self->_dataArray count]; i++)
    {
        NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0];
        NSLog(@"myIP.row %d",myIP.row);
        UITableViewCell *cell = [tblView1 cellForRowAtIndexPath:myIP];
        UIButton *btn = (UIButton *)[cell.contentView viewWithTag:BUTTON_TAG];
        NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag);
        UITextField *tf = (UITextField *)[cell.contentView viewWithTag:TEXT_TAG];
        NSLog(@"tf text %@, tag %d",tf.text,btn.tag);

    }
}

問題是您使用標記0設置視圖的子視圖,而視圖的標記屬性默認值為0, [someview viewWithTag:0]返回someview本身。

我這樣做的方式是。 我傳遞一個函數來處理TableViewController中的事件作為TableViewcell的委托,並注冊該事件以調用委托函數,並將其回調。 我就是這樣做的

在tableview控制器cellForRow func中

        let cell = tableView.dequeueReusableCell(withIdentifier: "blablabla") as! FeedbackTableViewCell
        cell.buttonEventDelgate = buttonPressed
        cell.indexPath = indexPath //This is required to find which cell button was pressed
        return cell

在TableViewcell中,當用戶單擊按鈕時,我會調用此委托

回到Tableviewcontroller,我將按如下方式實現委托

 @objc func buttonPressed(sender: UIButton, indexPath: IndexPath) -> Void {
    print("button pressed \(indexPath.row)")
  }

希望這可以幫助!!! 讓我知道是否有更好的方法來做到這一點

暫無
暫無

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

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