UITableView didSelectRowAtIndexPath not working after upgrade to iOS8 - ios

I have an iOS app with several UITableViews in them all of which worked as intended
. I upgrade the app to handle iOS8
since then I had a problem with loading a custom cell into the table view who's nib had the box in the ib checked 'use auto layout'. I then uncheck all of these on my custom cell and since then the cells of all my UITableViews not only don't call didSelectRowAtIndex path method but are not highlighted on touch.
I have check that all the cells are active by adding
if(cell.userInteractionEnabled){NSLog(#"is enabled");}else{NSLog(#"is not enabled");}
all of the loaded cells write 'is enabled' to the log
I am setting the delegate and data source via the ib in the storyboard and all of this was working prior to me changing the 'use auto layout' and upgrade to run on iOS 8.
what have i missed?
here is my code to create the cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCellWithNumberCellIdentifier";
if( events.count>indexPath.row &&[[[events objectAtIndex:indexPath.row] objectForKey:#"tag"] integerValue] == -1)
{
EventsMonthSeparator *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell.translatesAutoresizingMaskIntoConstraints=NO;
cell = (EventsMonthSeparator *)[EventsMonthSeparator cellFromNibNamed:#"EventsMonthSeparator"];
cell.date.text=[[events objectAtIndex:indexPath.row] objectForKey:#"date"];
[Functions setFontFamily:#"Neutra Display" forView:cell andSubView:YES];
if(cell.userInteractionEnabled){NSLog(#"is enabled");}else{NSLog(#"is not enabled");}
}
return cell;
}else
{
eventsRow *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell.translatesAutoresizingMaskIntoConstraints=NO;
cell = (eventsRow *)[eventsRow cellFromNibNamed:#"eventsRow"];
cell.description.text=[[events objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.timedate.text=[[events objectAtIndex:indexPath.row]objectForKey:#"date"];
cell.image.image=[UIImage imageNamed:#"tiledrinks"];
cell.status.text=[Functions statusForIndex:[[[events objectAtIndex:indexPath.row] objectForKey:#"booked"] intValue]];
[Functions setFontFamily:#"Neutra Display" forView:cell andSubView:YES];
cell.tag=1;
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
if(cell.userInteractionEnabled){NSLog(#"is enabled");}else{NSLog(#"is not enabled");}
}
return cell;
}
}

Please Do check that Auto-Layout is OFF (Non selected tickMark) in your Custom cell's xib Also.
And in Your TableView check this default setting,
As attached in below image
I have same issue once, where I have give NO Selection in selection property of TableView's attribute inspector.
If you have done same then give Single selection there.
Hope this will help you !

If you have selected auto layout, cell.translatesAutoresizingMaskIntoConstraints=NO; will negate the auto layout that Xcode provides.
Also, could you check if all your tableViews' delegates are intact? If your delegate isn't set, the tapping on table view cell will not invoke the 'didSelectRow' method.

You can select "in single selection During Editing" in TableView,
for example:

Related

UITableViewCell detailTextLabel disappears when scrolling

I'm using an array of strings where I set the detailTextLabel from. Initially all subtitles are set correctly but if I scroll the detailTextLabel disappears.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"personCell" forIndexPath:indexPath];
Person *person = [_persons objectAtIndex:indexPath.row];
cell.textLabel.text = person.name;
cell.detailTextLabel.text = person.phone;
// also tried setNeedsLayout but does not help
[cell setNeedsLayout];
return cell;
}
I'm using an iPhone 6 and iOS 8. I'm also using storyboard and set the UITableViewCell style to Subtitle.
OK, now that we've found the problem (with the nil phone number text on the person) you could solve it a couple of ways.
It seems that you don't want to set the text to blank. I imagine this is due to the fact that it lays out the cell in an odd way with the title pushed up to the top but nothing underneath it. Understandable.
So, you could create a custom UITableViewCell subclass. In it you can manage the layout yourself and if the number is nil lay it out one way and if it has a phone number lay it out a different way.
An easier way would be to use two different prototype cells instead.
In the storyboard create two prototype cells.
One with type Basic and give it a reuseIdentifier noPhoneNumberCell.
The other with type Subtitle and a reuse identifier phoneNumberCell.
Then in the code you can do something like this...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Person *person = [_persons objectAtIndex:indexPath.row];
UITableViewCell *cell;
if (person.phone) {
cell = [tableView dequeueReusableCellWithIdentifier:#"phoneNumberCell" forIndexPath:indexPath];
cell.detailTextLabel.text = person.phone;
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"noPhoneNumberCell" forIndexPath:indexPath];
}
cell.textLabel.text = person.name;
return cell;
}
This will now create two queues of cells. One for people with phone numbers and one for people without.
This way you don't mix the two and so avoid the problem you are facing.
[cell.detailTextLabel sizeToFit];

UITableViewCells reverting back to default or other values

So I've created a TableView as below, but I have quite an annoying problem.
When I come to this VC, I click Choose User, and I selected user Atoshum.
When I scroll down, this top cell goes off screen as I scroll through the bottom cells.
When I scroll back up, the cell has reverted to a default (or occasionally, takes the value of another cell).
I make the cells as such.
static NSString *CellIdentifier = #"Cell";
DMSDrugInstanceCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!Cell) {
Cell = [[DMSDrugInstanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cell.chooseUserButton.tag = indexPath.row;
[Cell.chooseUserButton addTarget:self
action:#selector(performSegue:)
forControlEvents:UIControlEventTouchUpInside];
return Cell;
In the cell creation you dont ever set any values. All you do is set the tag and then add an event target. If you want it to keep the choose, you need to store/save that choice when it is made and then in the cell create, set it based on that saved value.
you are reusing table cell. So every time your cell reload then you need to set value in cell.
in cellForRowAtIndexPath method , you need to set value in cell according to index path.
This is because the UITableView do not create the new cells for total number of elements. Rather it re-uses the cells which are off the visibility. Hence you feel that your data is reset or getting reflected on some other cell.
The best approach is to store all your data in some Array (let it be tagDataArray) and then set your cells as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DMSDrugInstanceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DMSDrugInstanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cell.chooseUserButton.tag = [tagDataArray objectAtIndex:indexPath.row];
...
...
return Cell;
}

UITableView - reusing cells while in editing mode

I'm finding that if you set a table view into editing mode, upon scrolling the table after deleting a row the cell edit control (the red minus sign on the left) is in a random state (turned vertical or horizontal) on the rest of the cells as you scroll the table. Presumably because I'm reusing cells like this:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
How can I force the edit control to be in the correct state for each cell? It should always be in the default horizontal state unless I tap it to delete a cell.
EDIT: Here's the cell code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"WhateverIdentifier";
MyCell *cell = nil;
//This IF statement fixes the problem, but then I'm not reusing the cells
if (!tableView.isEditing) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil] objectAtIndex:0];
}
//customize cell
return cell;
}
Are you calling [super prepareForReuse] in the method prepareForReuse of your custom cell?
That resolved my problem.
I just checked in a UITableView I had handy, and I don't see that problem. I'm using dequeueReusableCellWithIdentifier: as you are (without the if statement, of course). Are you doing something special with swiping, or deleting multiple rows or something?
(I'd make this a comment but can't yet. I'll delete it once you've resolved your problem.)

First visible UITableView rows not showing data when loading...but then do on scroll

I have a UITable which shows the 10 most recent images from a web service. Each row has its own image. When the UITable first loads in the viewcontroller it doesnt show the first 4 images in the rows (screen is blank). If I scroll down the last 6 images appear....then if I scroll back the first 4 images that were NOT there originally DO appear and everything looks the way I wanted it to initially. My guess is its something do with the way cells are reused.
Here is my tableView code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[cell.contentView addSubview:[self.photoList objectAtIndex:indexPath.row]];
return cell;
}
What solved my issue was adding the following line to my method that deals with streaming the photos from the Web API:
[_tableView reloadData];
I noticed that if I moved the Table View Cell up or down in the its Table View at the Storyboard that changed the position of the invisible row as well. So I solved the problem by setting the Table View Cell as hidden.

When creating my UITableViewCells, why is the init never called? (Resulting in blank cells?)

In my UITableView, I recently changed the structure of the cell from formerly just putting UILabels in the contentView of the cell, to adding two UIViews (CellFront and CellBack, on top of one another) into the contentView (so I can achieve a sliding effect by sliding the top one off and revealing the lower one) and adding the UILabels to the top UIView.
However, now, for whatever reason, the cells never get init'd and as a result my UITableView is full of blank cells.
My cell gets created as follows (ArticleCell is a subclass of UITableViewCell):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = nil;
ArticleInfo *articleInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Checks if user simply added a body of text (not from a source or URL)
if ([articleInfo.isUserAddedText isEqualToNumber:#(YES)]) {
CellIdentifier = #"BasicArticleCell";
}
else {
CellIdentifier = #"FullArticleCell";
}
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// If the user simply added a body of text, only articlePreview and progress has to be set
cell.preview = articleInfo.preview;
// If it's from a URL or a source, set title and URL as well
if ([articleInfo.isUserAddedText isEqualToNumber:#(NO)]) {
cell.title = articleInfo.title;
cell.URL = articleInfo.url;
}
return cell;
}
But I set a breakpoint on the initWithStyle method above within the if statement and it never gets called:
What would cause this? I'm deleting the app and building it from scratch every time, so data is definitely being added to the UITableView, but all the cells are blank. And I can tell a bunch of cells are being added as I have disclosure indicators on all of them, and the table view just gets filled with empty cells with the indicators only.
What am I doing wrong?
try
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
instead of
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
the first one is the old standard way. It will not create a cell for you. While with the second a cell will be created form the storyboard. So if you use storyboards you should use indeed the method you are using now, but it will never go info the if branch, as the cell will never be nil.
when instantiating form storyboard, initWithStyle:reuseIdentifier: is never called. Either set everything up in -initWithCoder: or -layoutSubviews

Resources