如何在单击按钮上获取UITableviewcell(自定义单元格)值

时间:2013-07-17 08:21:43

标签: ios objective-c uitableview

我有一个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'

7 个答案:

答案 0 :(得分:11)

我认为,一旦从btn获取,您就可以直接访问您的单元格的textField2cellForRowAtIndexPath:属性。假设您正在创建并返回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);
}

希望有所帮助!

答案 1 :(得分:2)

简单的方法:

-(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);

}

答案 2 :(得分:1)

以下代码获取indexPath by&#34; event&#34; param可能会更好:

-(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:)

答案 3 :(得分:0)

通常当您收到unrecognized selector错误时,因为您正在访问已在内存中替换的对象。在您的情况下,您可能正在访问不可见的单元格,因此contentview会返回nil

执行for循环时,您似乎可以访问所有不可见的单元格。对我来说,您只能访问可见的单元格contentview为零,因此访问titleLabel会出现无法识别的选择器错误。

答案 4 :(得分:0)

为标记值添加填充。否则,第一行标记为0且与内容视图标记匹配,默认情况下所有视图标记均为0。因此,当标记等于0时,为什么会出现错误的视图。

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

但是,我会将解决方案更改为不使用增量标记而是使用静态标记。您已经拥有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);

    }
}

答案 5 :(得分:0)

问题是您使用标记0设置视图的子视图,而视图的标记属性默认值为0,[someview viewWithTag:0]返回某个视图本身。

答案 6 :(得分:0)

我这样做的方法是。我通过一个函数来处理TableViewController中的事件,作为TableViewcell的委托,并注册该事件以调用委托函数,然后将其回调。这是我的方法

在表视图控制器中,cellForRow函数

        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)")
  }

希望这会有所帮助!!!让我知道是否有更好的方法

相关问题