Custom Cells Overlapping in UITableView - ios

I have a custom cell class called GameCell, the UI being created in storyboard. When my cells load, they load on top of each other. This problems occurs for cell.clipsToBounds YES and NO, just in different variations:
I have also tried [cell setClipsToBounds:(BOOL)] with no success.
Here's my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"gameCell";
GameCell *cell = (GameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView.superview setClipsToBounds:NO];
NSLog(#"made a cell");
PFObject *myPartners = [self.games objectAtIndex:indexPath.row];
PFUser *partner = [self.partnerList objectAtIndex:indexPath.row];
// Cell profile image
cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.width / 2;
cell.profileImage.clipsToBounds = YES;
if([partner objectForKey:#"profilePic"]!=nil){
cell.profileImage.image = [partner objectForKey:#"profilePic"];
}
else {
cell.profileImage.image = [UIImage imageNamed:#"smile_green.png"];
}
//Cell indicators
if((int)[myPartners objectForKey:#"sent"]==1){
cell.myIndicator.image = [UIImage imageNamed:#"arrow_icon_dbl.png"];
}
else if ([myPartners objectForKey:#"sent"]==0){
cell.myIndicator.image = [UIImage imageNamed:#"arrow_icon.png"];
}
cell.partnerName.text = [myPartners objectForKey:#"receiverName"];
cell.gameId = myPartners.objectId;
// Cell drop shadow
[cell.cellView.layer setShadowColor:[UIColor blackColor].CGColor];
[cell.cellView.layer setShadowOpacity:0.5];
[cell.cellView.layer setShadowRadius:2.0];
[cell.cellView.layer setShadowOffset:CGSizeMake(1.0, 1.0)];
// Cell buttons
if([myPartners objectForKey:#"picture"]!=nil) {
[cell.myPlay setEnabled:NO];
} else {
[cell.myPlay setEnabled:YES];
}
return cell;
}
The view is a UITableViewController embedded in a NavigationController

You have used dequeueReusableCellWithIdentifier:
use like this
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
and also check this link.
UITableView cell Contents are disappearing and overlapping when scrolled in UITableViewcell?

You should make sure that the height of your cell is the RowHeight of your UITableView control.
e.g.,
Cell is designed from XIB, and the height of the view is 30, then make sure:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0f;
}

In attributes inspector cancel Adjust Scroll view insets option.
you need clicked view controller .

Related

Adding UIDatePicker to cell content view

I'm attempting to put an instance of UIDatePicker in the accessory view of a UITableView cell, and have been following this SO thread as a template. However, it looks as if the picker is being placed above the cell entirely:
Below is the code I'm using to try to add a Date Picker to the accessory view of a UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellNewRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RowCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
switch (indexPath.row) {
case EmployeeOvertimeRow:
cell.textLabel.text = NSLocalizedString(#"Test", #"One");
_datePicker = [[UIDatePicker alloc]init];
_datePicker.datePickerMode = UIDatePickerModeTime;
//OLD: cell.accessoryView = _datePicker;
//POST EDIT
[cell.contentView addSubview:_datePicker];
break;
default:
break;
}
return cell;
}
Does anyone have any guidance on what I'm doing wrong or how to fix this?
The accessoryView of a UITableViewCell is surely not what you think : it's the little view at the right of the cell, typically, it's an arrow, and it's pretty small, not made to be the width of the screen at all. You should try adding your view to the contentView. You will need to set a bigger height for your cell in the heightForRowAtIndexPath method, too.

Xcode TableViewCell is reusing content upon button click

I realize this is a common problem but I'm just not getting it. I have a button that when pressed on a cell, changes to a check mark. However, when pressed it changes other reused cell's image to a check mark as well. I'm using a cell's delegate to manipulate local data on the tableView's VC.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell...
static NSString *ReusableIdentifier = #"Cell";
SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];
cell.delegate = self;
if ([self.selectedRows containsIndex:indexPath.row]) {
[cell.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
}
else {
[cell.plusButton setBackgroundImage:plusImage forState:UIControlStateNormal];
}
cell.tag = indexPath.Row
return cell;
}
Method in custom cell's class.
- (IBAction)addSongButtonPressed:(UIButton *)sender
{
[self.delegate addSongButtonPressedOnCell:self];
UIImage *checkImage = [UIImage imageNamed:#"check.png"];
[self.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
}
Method from the cell's delegate.
-(void)addSongButtonPressedOnCell:(id)sender
{
NSInteger index = ((UIButton *)sender).tag;
NSMutableDictionary *track = [self.searchTracks objectAtIndex:indexPath];
[self.selectedRows addIndex:indexPath];
}

UITableViewCell with UIScrollView cause content of other cells disappearing

I was trying to find solution almost everywhere, but I didn't find it. So, here is my problem.
I have UITableView with custom UITableViewCells.
The first cell has UIScrollView inside its Content View.
The Second cell has UILables and other basic views inside its Content View.
So, if there is UIScrollView inside the first cell, content of the second cell disappears. It appears only if the first cell scrolls out of the tableView frame.
Can anybody help me figure it out? Thank you.
Code preview
#pragma mark - UITableView Data Source
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_photosIndexPath]) {
static NSString *PhotosCellIdentifier = #"AdDetailsPhotosCell";
BazarAdDetailPhotosCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotosCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailPhotosCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PhotosCellIdentifier];
}
cell.photoScrollView.scrollsToTop = NO;
cell.photoScrollView.delegate = cell;
[cell setPhotos:_adDetail.photos];
return cell;
}
else if ([indexPath isEqual:_adDetailsPath]) {
static NSString *DetailsCellIdentifier = #"AdDetailsDetailCell";
BazarAdDetailsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailsDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsCellIdentifier];
}
cell.adTitleLabel.text = _adDetail.title;
cell.priceLabel.text = _adDetail.price;
// this cell content disappears
}
}
Might be issue with cell drawing on iOS 7.1, according answer on iOS 7.1 beta5 tableviewcell height showing objects outside it's range, try to clip subviews:
cell.clipsToBounds = YES;
Try it
#pragma mark - UITableView Data Source
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_photosIndexPath])
{
static NSString *PhotosCellIdentifier = #"AdDetailsPhotosCell";
BazarAdDetailPhotosCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotosCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailPhotosCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PhotosCellIdentifier];
}
cell.photoScrollView.scrollsToTop = NO;
cell.photoScrollView.delegate = cell;
[cell setPhotos:_adDetail.photos];
return cell;
}
else {
static NSString *DetailsCellIdentifier = #"AdDetailsDetailCell";
BazarAdDetailsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailsDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsCellIdentifier];
}
cell.adTitleLabel.text = _adDetail.title;
cell.priceLabel.text = _adDetail.price;
// this cell content disappears
return cell;
}
}

Set frame size image inside UITableView

I have an image size : CGRectMake(100,70,50,50) inside cellForRowAtIndexPath. I need to change the frame size image when the cells is odd
Code is:
if(indexPath.row %2)
{
CGRectMake(200,70,50,50)
}
else
{
CGRectMake(100,70,50,50)
}
In the first time when I load TableView, it's work ok, but when I scroll again and again, the frame size image display is in the wrong position. It changes very funny, I don't know why.
Can you help me
Thanks a lot.
You have set set the frame on your UIImageView subview:
if(indexPath.row %2) {
cell.yourImageView.frame = CGRectMake(200,70,50,50)
} else {
cell.yourImageView.frame = CGRectMake(100,70,50,50)
}
You can do it following way.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIImageView * imageView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexpath.row%2 == 0)
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 70, 50, 50)];
else
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 70, 50, 50)];
[cell.contentView addSubview:imageView];
imageView.tag=11;
[imageView release];
}
}
else
{
imageView = (UIImageView*)[cell.contentView viewWithTag:11];
}
// here are couple of other statements
...............
}
Personally, I would create a custom table cell and do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ImageCell";
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureWithRow:indexPath.row];
}
Then in your custom table cell, create an IBOutlet for your resizable image view and add a method:
-(void)configureWithRow:(NSInteger)row
{
if(indexPath.row %2)
{
self.resizableImageView.frame = CGRectMake(200,70,50,50)
}
else
{
cell.resizableImageView.frame = CGRectMake(100,70,50,50)
}
}
This does a couple of things
You don't have to check for a nil cell because you are guaranteed to get a cell back dequeueReusableCellWithIdentifier as long as the identifier matches one in your storyboard and your view controller is a UITableViewController.
You move the logic for the configuration of the cell to the custom table view cell, which is where it really should be. The view controller shouldn't care about the size of the image in the cell.

iOS: UITableView mixes up data when scrolling too fast

I've subclassed the UITableViewCell to add custom appearance to it. At the init level of the MYTableViewCell I added 4 subviews: UIImageView, and three UILabel(s). All 4 subviews have a different Tag assigned to them.
Inside the cellForRowAtIndexPath method I either create a new cell if it wasn't available at first, or reuse available one and assign the proper text to the ui labels.
The problem I am having is that if I try to scroll super fast, then the data gets messed up, however if I scroll up and down more slowly, then everything works fine.
Any thoughts??
Below is the code:
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"itemListTableViewCell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
DisplayableEntity *displayableEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
if( ! cell ) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[self tableView:tableView appearanceForCell:cell withEntity:displayableEntity];
} else {
UIImageView *imageView = (UIImageView *) [cell viewWithTag:IMAGEVIEW_TAG];
imageView.image = [UIImage imageNamed:displayableEntity.displayImageName];
UILabel *titleLabel = (UILabel *) [cell viewWithTag:TITLEVIEW_TAG];
titleLabel.text = displayableEntity.entityName;
UILabel *itemDescription = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG];
itemDescription.text = displayableEntity.entityDesctiption;
}
}
// some code removed to make it brief
- (void)tableView:(UITableView *)tableView appearanceForCell:(MyTableViewCell *)cell withEntity:(DisplayableEntity *)entity {
// cell image view
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[entity displayImageName]]];
[cellImageView setTag:IMAGEVIEW_TAG];
[cell addSubview:cellImageView];
// adding entity name label
UILabel *itemTitleName = [self itemTitleNameLabelWithFrame:itemNameLabelRect itemName:[entity entityName]];
[itemTitleName setTag:TITLEVIEW_TAG];
[cell addSubview:itemTitleName];
// adding 'assigned to' label right under the item name label
UILabel *itemDescriptionLabel = [self itemDescriptionLabelWithFrame:descriptionLabelFrame itemDescription:[entity entityDesctiption]];
[itemDescriptionLabel setTag:DESCRIPTION_TAG];
[cell addSubview:itemDescriptionLabel];
}
I see some troubles in tableView:cellForRowAtIndexPath: logic
It should be:
Dequeue cell
If cell cannot be dequeued - create the new one
Set all cell properties
I mean something like this:
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"itemListTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} // <-- Note there is no else, we should reset properties in both cases
NSManagedObject *managedObject = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [managedObject valueForKey:#"text"];
cell.imageView.image = [managedObject valueForKey:#"image"];
return cell;
}

Resources