I have a UIButton in a customized UITableViewCell, which subclassing UITableViewCell.
The UIButton is IBOutlet-ed in IB, and IBAction-ed to a function:
- (IBAction)clickRead:(id)sender;
Now, when I click on the button, the button has no response, since the function has nothing inside. What should I write in the function so that the UIButton can simulate tableView's didSelectRowAtIndexPath function ( i.e. select the table cell ) ?
This answer might help: hitting a next button for a UITableViewcell
You can call UITableView selectRowAtIndexPath:animated:scrollPosition: to visually select the row, but you will still have to manage any events for yourself: loading another view, calling didSelectRowAtIndexPath:, storyboard segues, etc.
Related
In my project, there is a UITableView which contains UICollectionViews in each row. Each UICollectionView contains a UIButton. I implemented those by using a tutorial here:
https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
The problem is, I need to add an action to button click. The usual IBAction click works, but I can't pass any data except an integer (tag) to it.
The question is, how do I pass some data to my button click action?
UIButton in UICollectionViewCell
UICollectionViewCell in UICollectionView
UICollectionView in UITableViewCell
UITableViewCell in UITableView
UITableView in UIViewController/UITableViewController
so your button outlet must be on collectionviewcell class and your button's action must be in uicollectionviewcell, and you can pass data from your uicollectionviewcell to UITableViewCell with delegate (uicollectionviewcell-delegate), and UITableViewCell to UIViewController/UITableViewController with another delegate.
I have a UICollectionView in every UITableViewCell of my UITableView.
The program should perform a segue there is a click on the UITableViewCell but the cell is clickable just out of the UICollectionView.
It is clickable just in the red portions.
Do you have any ideas?
You need to disable user interaction in each collection view:
In your storyboard uncheck here:
Or in your cellForRowAtIndexPath:
cell.collectionView.userInteractionEnabled = NO;
What I'm trying to do should be pretty straight forward from my understanding, yet I seem to be having some trouble with this.
I'm trying to add UITextField to a UITableViewCell in my Storyboard, however it only lets me place the text field as the child of a UIView, with the latter placed in the table view cell.
ie:
UITableViewCell
|__UIView
|__UITextView
I create my IBOutlet in my Swift ViewController and add the following function to have the keyboard appear on touch
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.nameTextField.becomeFirstResponder()
}
When I run this though, the cell responds to touch however it's as if the text view is always hidden and never gets enabled or becomeFirstResponder. I'm presuming it has to do with the UIView in the way, however I've been unsuccessful in placing the text view directly inside the table view cell.
If anyone has an idea on what I'm doing wrong, I'd greatly appreciate the help,
Thank you!
Create a custom UITableView cell as a xib and make a corresponding .swift file for this. Add a textField to the cell and create an IBOutlet for the textField in the custom .swift class. Make sure your constraints are set in this xib.
In your ViewController .swift class, make the func tableView(cellForRowAtIndexPath) return your custom cell.
You do not have to set the firstResponder because the keyboard will come up by default. Unless of your cell is basically just a textField, it doesn't seem wise to have the didSelectRowAtIndexPath to always bring up the keyboard. If, for some reason that the cell always takes the touch action, then use didSelectRowAtIndexPath, but use
tableView.cellForRowAtIndexPath(indexPath).textField.becomeFirstResponder()
Set textField delegate to the custom cell and add this in the CustomTableViewCell.swift to dismiss the keyboard upon press of return key:
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
Side note: The keyboard will not show in the simulator unless you deselected the option to use the hardware keyboard.
Here is a sample project with what I think you are trying to achieve:
https://mega.nz/#!B1ASDIQT!8-diyv_Fd2H66nb08gs3ObzlQqVHKUUIs4U_BbI6s4s
In tableviewcells i have a buttons and webViews, with some array count.so when i tap on particular tableViewcells button action. i have to hide that particular tables cells webview?
You can do it like this, In UIButton action, you will get cell index if you've given tag to your button, using that tag, you can make cell object with NSIndexPath for the cell you've tapped, hide UIWebView subviews within that cell, you may need to set its height base on some condition which you've made in heightForRowAtIndexPath of UITableView. Another way to get the cell is, [button superview]; here you'll get containerview for particular cell, and apply the same logic, this would only work if you've added button and webview in contentview.
[cell.webView setUserInteractionEnabled:NO];
Make the webView can't get the user touch focus, so that the cell will get the user interaction : )
I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.