How to detect tap on Uitableview cell with uiview and uibutton?

后端 未结 3 1984
南方客
南方客 2021-01-25 22:12

I have created a table view and i am adding my custom uiview into the cell\'s content view. The uiview has a uibutton. However i can add the uibutton into the content view when

相关标签:
3条回答
  • 2021-01-25 22:17

    Are you sure two different events are being triggered by the same tap? My understanding is that UIKit generates only one UIEvent and sends it to the top-most view in the hierarchy that responds to that type of gesture. In this case, if the button is higher in the view hierarchy, as it probably is, it should be receiving the event message. But, I may be wrong.

    One solution to definitely avoid the possibility of two events being triggered, though possibly not the ideal, would be to deactivate row selection for the tableView, as follows:

            self.tableView.allowsSelection = NO;
    

    Then, add a view covering the remainder of the tableCell & add a gesture recognizer to that view. Since they don't cover the same area, there is no chance of conflicting events. Of course, to know what row was tapped, you'd have to add an instance variable for both the button and the new view to hold the indexPath. You would set the index path when you set up the tableCell in tableView:cellForRowAtIndexPath:

    Hope this is helpful or gives you some new ideas.

    0 讨论(0)
  • 2021-01-25 22:26

    You can add a Button in cell in as

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake( 13.0f, 13.0f, 90.0f, 90.0f)];
    [button addTarget:self action:@selector(BtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [cellView.contentView addSubview:button];
    

    then you can get events separately

    0 讨论(0)
  • 2021-01-25 22:26

    You could subclass UITableViewCell and add a button, here is an example class:

    import UIKit
    
    private let DWWatchlistAddSymbolCellAddSymbolButtonLeftMargin: CGFloat = 15
    
    class DWWatchlistAddSymbolCell: UITableViewCell {
    
      private(set) var addSymbolButton:UIButton
    
      init(reuseIdentifier:String?, primaryTextColor:UIColor) {
        self.addSymbolButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        super.init(style:UITableViewCellStyle.Default, reuseIdentifier:reuseIdentifier)
        selectionStyle = UITableViewCellSelectionStyle.None
        backgroundColor = UIColor.clearColor()
        imageView!.image = UIImage(named:"PlusIcon")!
        addSymbolButton.setTitle("Add Symbol", forState:UIControlState.Normal)
        addSymbolButton.setTitleColor(primaryTextColor, forState:UIControlState.Normal)
        addSymbolButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
        addSymbolButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
        contentView.addSubview(addSymbolButton)
      }
    
      required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }
    
      // MARK: Layout
    
      override func layoutSubviews() {
        super.layoutSubviews()
        addSymbolButton.frame = contentView.frame
        let imageViewFrame = imageView!.frame
        let imageViewMaxX = imageViewFrame.origin.x + imageViewFrame.size.width
        let addSymbolButtonX = imageViewMaxX + DWWatchlistAddSymbolCellAddSymbolButtonLeftMargin
        addSymbolButton.contentEdgeInsets = UIEdgeInsetsMake(0, addSymbolButtonX, 0, 0);
      }
    }
    

    You can see the "Add Symbol" button at the bottom:

                                           Add Symbol Button Cell

    When you're returning the cell for the UITableViewDataSource, make sure to set the target for the button:

    private func addButtonCell(tableView: UITableView) -> UITableViewCell {
      var cell:DWWatchlistAddSymbolCell? = tableView.dequeueReusableCellWithIdentifier(kDWWatchlistViewControllerAddButtonCellReuseIdentifier) as? DWWatchlistAddSymbolCell
      if (cell == nil) {
        cell = DWWatchlistAddSymbolCell(reuseIdentifier:kDWWatchlistViewControllerAddButtonCellReuseIdentifier, primaryTextColor:primaryTextColor)
      }
      cell!.addSymbolButton.addTarget(self,
        action:Selector("didPressAddSymbolButton"),
        forControlEvents:UIControlEvents.TouchUpInside)
      return cell!
    }
    
    0 讨论(0)
提交回复
热议问题