【问题标题】:UitableView Row selection and UIButton ActionUtableView 行选择和 UIButton 操作
【发布时间】:2013-09-15 10:37:30
【问题描述】:

我有一个带有五行和一个按钮的 UitableView。我设法为一行创建复选标记,我的意思是用户只能检查一行。当检查另一行时,前一个未选中。现在有人可以帮助我如何将我的按钮与行的选择联系起来。我尝试了一些方法,但没有成功。

这是我在 cellForRowAtIndexPath 中的代码:

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

还有我的按钮代码:

if (reloadSelectedRow==1) {

   PlayViewController * controller = [[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil];
    //controller.countdownLabel.text= score;
    [self presentViewController:controller animated:YES completion:nil];
}

}

reloadSelectedRow 是一个 int 变量,我创建它 .h 并在 didSelectRowAtIndexPath 中将其用作:

reloadSelectedRow = [[NSNumber alloc] initWithInteger:indexPath.row];

问题是当我按下按钮时没有任何反应。提前谢谢你。

【问题讨论】:

    标签: ios uitableview uibutton tableview


    【解决方案1】:
    [YourButton addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchDown];
    

    你可以像这样得到按下按钮的superView,以及索引路径:

     -(void)ButtonPressed:(id)sender
    {    
    
        //Get the superview from this button which will be our cell
        UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
        NSIndexPath *pathToCell = [tableView indexPathForCell:owningCell];
    
    }
    

    【讨论】:

    • incmiko 你能解释一下吗?我不明白在哪里使用第一种方法?
    • 在 cellForRowAtIndexPath 中。 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents。使用此方法,您可以向按钮添加操作。 developer.apple.com/library/ios/documentation/uikit/reference/…:
    • 我没有对我的 tableview 进行具体调用,或者我根本无法理解实现。无论如何,谢谢你的帮助。
    【解决方案2】:

    对于 swift 版本。我为每一行做了多项选择。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "PersonalTestTC", for: indexPath) as! PersonalTestTC
    
        cell.questionTitle.text = "some question here"
    
        cell.choiceButton1.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
        cell.choiceButton2.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
        cell.choiceButton3.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
        cell.choiceButton4.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
        cell.choiceButton5.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
    
        cell.choiceButton1.tag = 1
        cell.choiceButton2.tag = 2
        cell.choiceButton3.tag = 3
        cell.choiceButton4.tag = 4
        cell.choiceButton5.tag = 5
    
        return cell
    
    }
    

    使用标签来识别选择了哪一行和哪个答案

    func choiceButtonPress(sender: UIButton){
    
        let tbcell : UITableViewCell = sender.superview?.superview as! UITableViewCell
        let getCell : IndexPath = testListTable.indexPath(for: tbcell)!
    
        print(getCell.row)
        print(sender.tag)
    
    }
    

    对于 superview ,这取决于您在 UIButton 上的位置。

    【讨论】:

      猜你喜欢
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多