iOS 7 UITableViewCell issue - uitableview

I have an iOS 6 app that I am converting to iOS 7 using Xcode 5 in StoryBoard. Everything worked fine until I decided I wanted to add a custom UITableViewCell to a tableview controller after converting the app to iOS 7. I created the usual code to do this but when I run the app it gives an unexpected result whereby it just displays the contents of one cell row and it sort of floats above the tableview which shows what appears to be empty cell rows behind it. When I scroll the table rows, I see them scrolling in the background and the data being shown in the one view cell row changes while scrolling (see image attached). However, if I click on what looks like an empty row, it works correctly and it segues correctly to the detail view controller. I made the same changes to the app in iOS 6 using Xcode 4.6 on my other macbook and it works correctly (see image attached). So I'm thinking that iOS 7 handles the uitableviewcell class differently than in iOS 6 or I am making a mistake in the code in the iOS 7 app and not realizing it.
Here is the iOS 7 code that's not working correctly:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
ExhibitorsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[ExhibitorsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.myTableView){
NSManagedObject *object = [self.objects objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
else{
NSManagedObject *object = [results objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
return cell;
}
Here is the iOS 6 code that works (to me it seems to be the same, but I guess I need other eyes to view it):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
ExhibitorsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[ExhibitorsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.myTableView){
NSManagedObject *object = [self.objects objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
else{
NSManagedObject *object = [results objectAtIndex:indexPath.row];
cell.nameLabel.text = [object valueForKey:#"name"];
NSString * booth = [NSString stringWithFormat:#"Booth Number: %#", [object valueForKey:#"boothLabel"]];
cell.boothNumberLabel.text = booth;
}
return cell;
}
Here is the screen shot of what's happening in the iOS 7 app.
Here is what it should look like (this is before I made the custom UITableViewCell changes).

I figured it out. I had to drag the uilabel a little above the uitableviewcell until I see the cell boundaries highlighted. This placed the label above the cell. I then had to move the label back into the viewcell. Never had to do that in xcode 4.6. But, oh well.

Related

Custom cells in UITableView become blank after scrolling up or down

I was tasked to solve this bug in the app (It's for comercial use so I can't link the project). I'm also completely new to Objective-C or IOS development and I have no idea why the following happens. Reminder, I'm using custom cells.
When the tableview loads, everything looks fine.
Click here to see the example
But when I scroll up and back to the same postion the cell becomes blank, as if it had no data. Blank cell
If I repeat the same process everything looks fine again.
Here's the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cell2";
PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if (tableView==_table3)
{
NSString *docsDirr;
NSString *documentsDirectoryForSaveImages;
docsDirr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
documentsDirectoryForSaveImages = [docsDirr stringByAppendingPathComponent:#"/CarpetaImatges"];
NSString *match = #".jpg";
NSString *preCodigo;
NSScanner *scanner = [NSScanner scannerWithString:[pedarray objectAtIndex:indexPath.row]];
[scanner scanUpToString:match intoString:&preCodigo];
NSString *nomimg=[NSString stringWithFormat:#"%#.jpg", preCodigo];
UIImage *img = [UIImage imageWithContentsOfFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:nomimg]];
cell.img.image = img;
cell.layer.borderWidth=1.0f;
cell.img.layer.borderWidth=1.0f;
cell.cart.text = preCodigo;
cell.descart.text = [nomarray objectAtIndex:indexPath.row];
cell.cant.text = [cantarray objectAtIndex:indexPath.row];
cell.precart.text = [precioarray objectAtIndex:indexPath.row];
cell.pvpcart.text = [pvparray objectAtIndex:indexPath.row];
[cell layoutIfNeeded];
}
return cell;
}
I've debugged the method and everytime it returns a valid cell. When I click the empty cell it still works as expected. Essentially, the data is there, it's just not rendered for some reason.
I've checked other posts with the same/similar issue but nothing seems to really match. PrepareForReuse is a no go since there's no UI to be changed, just content. I'm getting the values of the arrays with indexPath.row to make sure I'm fetching the correct value. I've even tried to set the heightForRowAtIndexPath to the same height the cell should have (all cells are of the same size and never change) or let AutoLayout handle it but to no avail.
Code of numberOfRowsInSection (Returns the amount of orders that exists in database currently):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == _table3)
{
[self cargacarro];
return pedarray.count;
}
return pedarray.count;
}
Any ideas?
Register your cell nib in storyboard or in code before tableview is loaded, and after replace dequeueReusableCell... with this one
PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
cell.img...
cell.cart...
remove this part, is no longer needed
if (cell == nil)
{
cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}

UITableviewcell with Right Detail - TextLabel.text is always NULL

It's just very basic thing, I am using UITableview with cell style "Right Detail"
Now, in method cellForRowAtIndexPath when I do
cell.textLabel.text = #"Notification";
cell.detailTextLabel.text = #"Dates";
It never takes the text neither displays on the screen. When I log the cell text like
NSLog(#"%# -- %#",cell.textLabel.text, cell.detailTextLabel.text);
It prints the following line in console
(null) -- (null)
Here are some more details:
Xcode Version: 8.3.1
Simulator: iPad Pro (12.9 inch) iOS 10.3
Storyboard based tableview
Already selected "Right Detail" option from Style option.
Already tested the following
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier]
On the requests, here is the complete code for "CellForRowAtIndexpath"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
if (tableView == self.tableviewBusinessSettings) {
NSString *identifier = #"BCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];\
NSDictionary *dictSetting = [self.arrayBusinessSettings objectAtIndex:indexPath.row];
[cell.textLabel setText:[dictSetting objectForKey:#"key"]];
[cell.detailTextLabel setText:[dictSetting objectForKey:#"value"]];
}else if (tableView == self.tableviewNotifications) {
NSString *identifier = #"NCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.textLabel.text = #"Notification";
cell.detailTextLabel.text = #"Dates";
}
NSLog(#"%# -- %#",cell.textLabel.text, cell.detailTextLabel.text);
return cell;
}
Need quick help.
Thanks
Try checking the data in dictionary Like before sending it to cells of tableView just nslog() the data you are sending and other Thing try using
cell.textlabel.text = [dictSetting objectForKey:#"key"];
cell.detailTextLabel.text = [dictSetting objectForKey:#"value"];
and try replacing
UITableViewCell *cell = nil; to UITableViewCell *cell;

Cropped Text on TableViewController

(iOS 6.1.6, iPod Touch 4th Gen, Xcode 7)
Using the default "Subtitle" format for a TableViewController, I'm getting data that looks like this. Scrolling will sometimes refresh so it shows correctly, but not always. I've tried widening the labels in the prototype cell, but that doesn't seem to make a difference.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Creating Cell");
static NSString *tableID = #"locationOverviewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableID];
}
cell.textLabel.text = ((PullLocation *)WarehouseLocations[indexPath.row]).WarehouseLocation;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Items To Pull",(int)((PullLocation *)WarehouseLocations[indexPath.row]).NumItemsToPull];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Can anyone point me in the right direction?

how to make searchResultsTableView use custom cell instead of default cell [duplicate]

This question already has answers here:
iOS search bar not showing results
(3 answers)
Closed 8 years ago.
I have a uisearchbar that does a search successfully. The problem is that the searchResultsTableView doesn't show the results because it is using a regular UITableViewCell instead of my custom cell. I have set this custom cell in storyboards and connected the outlets to make sure everything is working properly.
My cellForRowAtIndexPath method looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"InviteCell";
InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[InviteTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
MyUser *person = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
person = [self.filteredContactArray objectAtIndex:[indexPath row]];
//This works and shows
cell.detailTextLabel.text = #"testing if using default cell";
}
else
{
person = [self.inviteContactsArray objectAtIndex:[indexPath row]];
}
//InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
person = [self.inviteContactsArray objectAtIndex:indexPath.row];
//these do not work and do not show in the results view
cell.nameLabel.text = person.fullName;
cell.emailLabel.text = person.email;
return cell;
}
This is what the results view looks like:
Currently there is no way for a prototype cell created in a storyboard to be registered in the searchResultsTableView.
A workaround is to put the cell into a separate xib file and register it for both the self.tableView and the self.searchDisplayController.searchResultsTableView using -[UITableView registerNib:forCellReuseIdentifier:].
For further information about this method see the documentation of UITableView.

Different UITableView scrolling performance on iPad 2 & iPhone 4s

Solved
There was a shadow on the self.view layer. After removed it, the tableview scrolls smoothly (56-59fps).
The problem I got is so weird.
I am building a UITableView based Twitter like App. After optimized the table view loading and scrolling, I tested the App (same code & same UI) on both iPad 2 and iPhone 4S. On the iPad 2, I got 56-59fps smooth scrolling animation. But on the iPhone 4S, I got only 43-49fps scrolling animation, which is awful.
This seems not caused by the Retina display. After I remove all images from cells, the problem is still there. The following is the code of cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// fetch note from the fetched results controller
Note *note = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([note.photos count] > 0) {
static NSString *CellIdentifier = #"Photo Cell";
PhotoCell *cell = (PhotoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
[cell clearContents];
}
NSNumber *currentRowNumber = [NSNumber numberWithInt:indexPath.row];
// check if the photos of this cell has been pre loaded
NSDictionary *coverImages = [self.coverImagesForNotes objectForKey:currentRowNumber];
if (coverImages == nil) {
if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {
coverImages = [self loadCoverImagesForCell:indexPath photos:note.photos];
[self.coverImagesForNotes setObject:coverImages forKey:currentRowNumber];
}
}
// assign contents to the cell
[self compositeContentView:cell forNote:note rowNumber:indexPath.row];
// refine
unsigned textContentHeight = [[self.cellTextHeightDict objectForKey:currentRowNumber] integerValue];
unsigned currentPageNumber = [self currentPageNumberAtRow:indexPath.row];
[cell initPhotoVideoView:textContentHeight photos:note.photos coverImages:coverImages showPage:currentPageNumber rowNumber:indexPath.row];
cell.delegate = self;
return cell;
} else {
static NSString *CellIdentifier = #"Simple Cell";
BaseCell *cell = (BaseCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[OneViewBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// assign contents to the cell
[self compositeContentView:cell forNote:note rowNumber:indexPath.row];
return cell;
}
}
Thanks for answers.
Are you using images in your table view cells? This will be more expensive on a retina display. Also if you are using images, try to load them in on a background thread which will reduce the latency.
If you're not using images then you should really look at how you are creating your cells, please post tableView:cellForRowAtIndexPath: method implementation

Resources