Width of UITableViewCell - xcode

I've project which is based on storyboard and on one of the views I've placed UITableView with UIViewTableViewCell (style Right Detail).
In Xcode everything looks OK, but when I launch application on simulator or device, the the table cell looks too wide.
Do you have any suggestions what could be reason of this strange behavior? What should I change to get cell be full visible?
EDIT:
I'm attaching cellforRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

Have you put the same width for your custom cell, as the width of a UITableView?
Also, have you managed your "Delete" Label size properly (using autolayout or autoresizing).
With new iPhone 6, we have to pay attention to widtch as well, just like with different heights, when iPhone 5 was introduced.
And I can see from those two screenshots you posted that you develop using iPhone6 sized ViewController and running it on iPhone5.
Im prety sure you didn't manage Autolayout / Autoresizing as you should.
Try doing this, for your "Delete" label:

I had the same problem where the content on the right hand side of my tableView was being cut off.
The reason being, I had set the Autolayout constraints correctly for the views inside my custom tableViewCell. However, I had not set the AutoLayoutConstraints for the tableView itself. Therefore the UITableView wasn't being constrained correctly and in my case was too large.

Related

Xcode editor canvas live auroresizing and autolayout for UITableViewCell

So this handy feature that I used so much just don't work with Autolayout ? Or how it is ? I cannot find any info anywhere about this, but for me it seems that it is not working with UITableViewCell because its content view is not resizing. And I have to check if I have correct layout at runtime and before with "springs and struts" a was able to move window in editor and see if it sizes layout correctly.
Best is not to use UITableViewCell in UI designer at all, design there view using autolayout and the add it to content view of cell. This code uses some custom code...
UITableViewCell *cell = [self dequeueReusableCell:[UITableViewCell className]];
if (!cell) {
cell = [UITableViewCell.alloc initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[UITableViewCell className]];
UIView *view = viewClass.create;
view.width = [cell width];
view.height = [cell height];
[cell.contentView addSubview:view];
}

UICollectionView poor scrolling performance with simple UICollectionViewCell

I'm using UICollectionView to show grid based View in iPad. I have only 200 (20rows x 10 columns) cells at a time on screen, each cell contain one UILabel only. It starts to get slower on scrolling and not smooth like in TableView.
I don't know the reason for this slower scrolling performance. How do I improve?
Thanks in advance!
I have 3 collectionviews, but the main content uicollectionview is the one which has been tagged 2. Here is my code
if (collectionView.tag==0) { //This tagged collection view is for Left side selection box
LeftSelectionBoxItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"SelectionCell" forIndexPath:indexPath];
return cell;
}
else if (collectionView.tag==1){ //This tagged collection view is for to show column names
TopColumnItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"ColumnCell" forIndexPath:indexPath];
cell.columnName.text = [NSString stringWithFormat:#"%#",[self.columnNames objectAtIndex:indexPath.item]];
return cell;
}
else if (collectionView.tag==2) { //This tagged collection view is for to show main content
GridItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"GridCell" forIndexPath:indexPath];
cell.dataLabel.text = [NSString stringWithFormat:#"%#",[[self.dataModel objectAtIndex:indexPath.section] objectForKey:[self.columnNames objectAtIndex:indexPath.item]]];
return cell;
}
else {
return nil;
}
Having a lot of UILabels is sometimes expensive. Try changing them to CATextLayers instead, as these have much less overhead. Note that if you do this and use rasterizing, you will need to tell the layer to rasterize for retina display devices too.

custom NSTableViewCell

I custom NSTableView just like what I do in UITableView. I implement the datasource and delegate. In the code, I do like this:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
ZJCellView *cellView = [tableView makeViewWithIdentifier:cellIdentifier owner:self];
//this is a custom view
if (cellView == nil)
{
CGRect rect = CGRectMake(0, 0, tableView.frame.size.width, kTableViewCellHeight);
cellView = [[[ZJCellView alloc] initWithFrame:NSRectFromCGRect(rect)] autorelease];
cellView.identifier = cellIdentifier;
// some other controls
}
return cellView;
I do the custom just like what I do in iOS.
The problem is there is a line in both left and right border, like:
I tried change the frame of the cell view and it seems to be of no use.
As it is the right way to custom the cell in iOS, I wonder where is it wrong in Cocoa.
Can any body help me?
thanks.
Finally, I find it is the Cell Space which can be set in the nib.
In iOS:
Tableviewcells have same with with Tableview. They are autoresized. If you set frame for a cell in cellForRowAtIndexPath: method, changes nothing.
For width you need to change the width of the tableview.
For height you need to implement delegate method heightForRowAtIndexPath: of the tableview or set rowHeight property.
But I don't know how is it for NSTableView, I think it is similar.

Custom UITableViewCell with large UIImageView causing scroll lag

I have a custom UITableViewCell that consists of a UIImageView and a UILabel. The cell is 320x104px and the imageView takes up the whole area with the label in front. There are only 8 cells.
in ViewDidLoad I am creating all needed images up front and caching them in a dictionary at the correct dimensions.
When I scroll the UITableView there is a noticable lag every time a new cell is encountered. This makes no sense to me as the image it is using is already created and cached. All that I'm asking of the cell is for its UIImageView to render the image.
I am using a custom cell with its view in a xib and configuring my UITableView to use it with:
[self.tableView registerNib:[UINib nibWithNibName:#"ActsCell" bundle:nil]
forCellReuseIdentifier:myIdentifier];
Cell creation and configuration:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* reuseIdentifier = #"ActsCell";
ActsCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(ActsCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Act* act = [self.acts objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.title.text = act.name;
cell.imageView.image = [self.imageCache objectForKey:act.uid];
}
What could be causing the lag? There would seem to be no benefit in trying to do anything Async as all the time-intensive work is done.
Do you load images from local files by any chance?
By using Instruments I've found that there is some lazy loading mechanism in UIImage - the real image data was decompressed from PNG only at the stage of rendering it on the main thread which caused the lag during scrolling.
So, after the loading of UIImage with -initWithContentsOfFile: method, I've added code to render the contents of this image to offscreen context, saved that context as new UIImage and used it for UIImageView in UITableViewCell, this made the scroll to be smooth and pleasant to the eye again.
In case of reference there is the simple code I'm using to force reading of image contents in separate thread (using ARC):
UIImage *productImage = [[UIImage alloc] initWithContentsOfFile:path];
CGSize imageSize = productImage.size;
UIGraphicsBeginImageContext(imageSize);
[productImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
productImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
And I think the surface of UIImage created in such a way will be in the supported format suited for rendering, which will also offload work needed to render it on main thread.
EDIT: The docs for UIGraphicsGetImageFromCurrentImageContext() say that it should be only used from main thread, but searching on the net or SO shows that starting from iOS 4 the UIGraphics.. methods became thread safe.

How to hide UIImageView on the cell when it is swiped to delete

I am implementing this. But the image is not hiding. What is the proper way?
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
{
PlayerCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PlayerCell"];
cell.ratingImageView.hidden= YES;
return;
}
You have to get the cell that was selected using that IndexPath, and then HIDE the imageview from that cell. Im pretty sure the apple tutorials have an example of getting the selected cell.
You need to get the actual cell at the index path, see if the following replacement does the trick:
PlayerCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Cheers

Resources