userInteraction is enabled but button action isn't called - ios

I am new to iOS programming. My cell is in a .nib. This cell displays an image which is working fine. I don't want the cell to perform any action or be selected. So, I have the following code :
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.contentView.userInteractionEnabled = YES;
My problem is I have a button which isn't called on touch. I have an action defined in the cell.m file. But nothing happens. I also tried adding the button programmatically, but in vain. I will appreciate any help or pointers as I have tried this for past few hours. I am pretty sure I am doing something silly.
Update:
This is how my cell xib structure looks -
There cell.nib, cell.m and cell.h. There is MainViewController which uses dequeueReusableCellWithIdentifier method to get the cell. This works fine as I am able to see the cell with the correct image. Action method is defined inside cell.m
Here is the xib file
https://www.dropbox.com/s/mzqzi6iz8lkbx2f/SHTableCell.xib
Thanks.

After checking the SHTableCell.xib you shared, it seems you have prevented user interaction on the cell itself so enabling user interaction on it's contentView / subViews will not make a difference.
In your xib, select "Test Cell" and check "User Interaction Enabled"
Also, it seems you haven't specified a re-use identifier to the cell.
It would be better if you specified one so that your -cellForRowAtIndexPath: could properly re-use the cell.
So... if you have something like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
SHTableCell *cell = (SHTableCell*)[tableView dequeueReusableCellWithIdentifier:#"SomeIdentifier"];
//...
}
then, "SomeIdentifier" is what should be in the xib in the first place.

Related

Dynamic control elements on a re-usable UITableViewController

I'm trying to reuse a UITableViewController for multiple purposes. My problem is that I'd like to display different buttons and other elements depending on the intention for displaying the list.
I'm currently using multiple cell prototypes to display different info for each item's detail, but I'd also like to be able to swap the controls depending on intention. I've been showing and hiding controls to accomplish this, but looking at the storyboard gets a bit ugly.
I was thinking maybe using a container view... just figured I throw this out there and see if anyone is doing anything similar. I didn't want to created separate list views just to change buttons.
Without more details, it's hard to say whether this will solve your problem particularly. However, I do precisely this in my current project and this is how:
I am going to assume the following. Let me know if they don't hold true and I'll update my answer accordingly.
You're creating a cell prototype for every permutation and combination, including actions. And so you have too many prototype cells.
Your actions are 'buttons' or similar controls on the cell (and not the swipe to reveal edit actions).
Create a custom class for your cell. Add a container for your actions and position it appropriately in story board. Connect the container to an outlet in the cell.
#interface MyCustomCell : UITableViewCell
#property MyCellTypeEnum type;
-(void) configure;
#end
#implementation MyCustomCell
-(void) configure {
switch(type) {
case type1:
// add actions to container
break;
case type2:
// etc.
}
}
#end
And in your TableViewController's cellForRowAtIndexPath do the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ... dequeue appropriate cell
cell.type = <appropriate type>
[cell configure];
return cell;
}
Hope that helps.

How to make that all the pictures were displayed?

I have TableViewController and ViewController. In 3 cells change pictures depending on the index and intForString in ViewController.
In the first session I work with all cells. In the second I work with one cell. When I go to the third session image stay only to the cell in which I worked the second session. And the other two images disappear. How to make that all the images were displayed?
I don't understand what you are asking when you say "How to make that all the images were displayed?"
However, this bit is very wrong:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
The table view data source method tableView:cellForRowAtIndexPath: is supposed to configure and return a new (or recycled) table view cell.
The method you're calling in the first line is the table view instance method cellForRowAtIndexPath. That method only returns a cell if there is a cell on-screen for the specified indexPath.
That won't work.
You want to use code that calls dequeueReusableCellWithIdentifier:forIndexPath. If you've defined a class for your identifier, that method will always return a valid cell. If not, you need to add code that checks for nil and creates a new cell using the UITableViewCell method initWithStyle:reuseIdentifier:

Best way to create custom UITableview section header in storyboard

Current I am creating a prototype cell in storyboard and using this cell as a section header.
Inside tableView:viewForHeaderInSection: method, I am dequeuing the cell and returning it.
My section header cell has a UITextField and a UIButton in it.
When I tap on text field keyboard appears but as soon as focus is moved away from text field whole section header disappears.
This happens when I return the cell directly as section header view, but if I return a newly allocated UIView as section header view onto which cell is added as subview then everything works fine besides autoresizing masks.
Why header is disappearing?
I am not sure what could be the best thing todo here.
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = #"SectionHeader";
SettingsTableViewCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//return sectionHeaderCell; // returning cell directly, section header disappears when focus is moved away from text field.
UIView * headerView = [[UIView alloc] initWithFrame:sectionHeaderCell.frame];
[headerView addSubView:sectionHeaderCell];
return sectionHeaderCell;//header view never disappears, but auto resizing masks do not work. Need to know how to set autoresizing masks to headerView so that it resizes correctly.
}
Prototype cell table views only allow you to design cells in the storyboard editor, not section headers and footers. Your attempt to use a UITableViewCell as the section header is a clever hack, but it's just not supported by the classes involved—UITableViewCell is not designed to be used for anything other than a table view cell. It could do a lot worse than the view disappearing or not being laid out correctly; UIKit would be well within its rights to fail an assertion, delete all the app's data, revoke your developer certificate, or set your house on fire.
If you want your code to function properly, your choices are to either create your section headers in code or to put them in a separate XIB file. I know that's not what you want to do, but those are the options you have.
I had the same issue and the fix was to return the cell's contentView like:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = #"SectionHeader";
SettingsTableViewCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
sectionHeaderCell.myPrettyLabel.text = #"Greetings";
sectionHeaderCell.contentView.backgroundColor = [UIColor whiteColor]; // don't leave this transparent
return sectionHeaderCell.contentView;
}
And you get the same autolayouted results as before, but without the disappearing.
I am sure you can use UITableViewCell as a section header, because UITableViewCell is subclass of UIView, so according to LSP
“objects in a program should be replaceable with instances of their
subtypes without altering the correctness of that program.”
In iOS 8, it's simple really. Just design your header the same way you design your cell. Everything is the same, you can put custom class and don't forget to add reuse identifier.
When it comes to use it in the code, just return that cell in tableView:viewForHeaderInSection method.
Don't forget to implement tableView:heightForHeaderInSection if you want to use fix height or tableView:estimatedHeightForHeaderInSection if the height depends on the cell intrinsic size.

Prototype Cell not being used by tableview

I have read many posts about similar problems but nothing seems to work, I am obviously doing something wrong. I have a TableViewController that is in a StoryBoard (XCode 5). For the PrototypeCell I set the type to custom and set the Identifier to "pbvcell". I added some labels, changed the background etc.. Here is my tableview delegate method for setting the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"pbvcell"];
// Configure the cell...
PBVlead *lead = [self.leads objectAtIndex:indexPath.row];
NSLog(#"Cell class %#", [cell class]);
UILabel *leadNameLabel = (UILabel *)[cell viewWithTag:1];
leadNameLabel.text = lead.leadName;
return cell;
}
Now the app launches but even after I add an object to the tableview datasource array and do a reload data, the cells are blank, like the custom cell is not being used. It looks like this should be easy and thats all I need to do. What on earth am I missing?
I left this in a comment above but just to keep things tidy I will post it as an answer here. It is silly but it is good to note that you have to manipulate the contextView of a prototype cell and not the tableview cell itself in order for your visual changes to have an effect...
"Because I embrace my own stupidity I will tell everyone what was going on here. I had set the TableCell background to blue and added some UILabels and set there color to white to show up against the blue background. Run the app, no labels.... What I finally realized is, I had not set the Content View background to blue. So..... What was happening was the labels actually are shown in the content view in the view hierarchy. White labels on a white background equals, invisible... :-) I set the content view background to blue and wola, there is everything! :-) Brother..?

IBActions for UITableViewCell prototype

So far, have read a few posts, such as this and this, but they have not really helped with my situation.
I'm creating a dynamic form for iPad using 'plain' style UITableViews. I have multiple different UITableViews on the page, so I defined a separate object to server as my datasource and delegate. I understand how to change the text of each cell using the datasource; however, I have no clue how to link the UITextFields in my prototype cells to an IBAction. I could figure out how to create a single IBAction for all textfields in my table, such that they all update the same data, but I don't know how to have each UITextField have a one-to-one correspondence with my datasource.
Here is my prototype cell:
and my code thus far:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myPrototypeCell"];
UILabel *buildingNumber = (UILabel *)[cell viewWithTag:100];
buildingNumber.text = [#"Building " stringByAppendingString:self.dataSource[indexPath.row][#"buildingNumber"]];
return cell;
}
self.dataSource is an NSMutableArray of NSMutableDictionaries.
Any help whatsoever is appreciated.
I initially thought you were referring to IBOutlets so my previous answer is somehow wrong but the inherent idea is still the same.
You cannot have IBActions or IBOutlets from a prototype cell unless the cell is subclassed. You can do so if the cells are static though, not that it can help in your case. Subclassing the UITableViewCell is not too hard or too bad, in fact if in the future you want to speed things up on your TableView, that is one of the many ways to start.
This tutorial provides a few different options for dealing with information inside a table view cell:
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
I almost always use a UITableViewCell subclass to deal with outlets and actions inside the cell. But this should be a decision you make based on your own architecture.
Hope this helps!
you need single for all your textField.So do the following:
Get the text field as your are getting label
UITextField *yourTextField = (UITextField *)[cell viewWithTag:101];
[yourTextField addTarget:self action:#selector(clickTextField:) forControlEvents:UIControlEventEditingChanged];
clickTextField method will get invoke for every Text Field.
Hope this helps.
Edit: Forgot to mention, you can you set delegate of UItextField and get a notification in UITextFieldTextDidChange: delegate method

Resources