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 uib开发者_StackOverflow社区utton into the content view when the cell is created.
I want to get the tap event on the tableview to perform some action. I also want tap event on the uibutton to perform a different action.
The problem is when i tap a row the button is also getting pressed and two events are triggered. How can i get both the events discreetly? i.e tap on the tableview cell when portion the cell outside the uibuttons boundary is clicked.
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
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.
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:
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!
}
精彩评论