30

Custom UITableViewCell Does Not Appear Correctly

 4 years ago
source link: https://www.codesd.com/item/custom-uitableviewcell-does-not-appear-correctly.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Custom UITableViewCell Does Not Appear Correctly

advertisements

I'm trying to create and use a custom UITableView cell in my table view, but it's acting funky. When I run the app the table looks like this:

qpACQ.png

This is correct. However, when I select a cell, the view shifts to this:

u53I1.png

As far as I can tell, it seems after selecting the cell that the cell view shifts to a default UITableViewCell layout with my star image behind it... If I proceed to select each cell I get this:

AzRyQ.png

As you can see, the star image (my custom view) only shows up (albeit screwy) when I select the cell again.

I've gone through Apple's tutorial when trying to make the custom cell so I know I'm doing everything I'm supposed to, but nobody else on StackOverflow has had a similar problem...

Here is the code for my project.

TableView Controller Header

//
//  TableViewController.h
//  CustomCell
//
//  Created by Jordan Gardner on 1/29/14.
//  Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@end

TableView Controller Implementation

//
//  TableViewController.m
//  CustomCell
//
//  Created by Jordan Gardner on 1/29/14.
//  Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import "TableViewController.h"
#import "CustomCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

#pragma mark - Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    //  Configure cell...
    cell.textLabel.text = [NSString stringWithFormat:@"Item %@", [NSNumber numberWithInteger:indexPath.row]];
    cell.imageView.image = [UIImage imageNamed:@"icon_folder.png"];

    return cell;
}

@end

CustomCell Header

//
//  CustomCell.h
//  CustomCell
//
//  Created by Jordan Gardner on 1/29/14.
//  Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *favoriteButton;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

CustomCell implementation

//
//  CustomCell.m
//  CustomCell
//
//  Created by Jordan Gardner on 1/29/14.
//  Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import "CustomCell.h"

@implementation CustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

So, any suggestions as to how I can prevent the views from acting so screwy would be greatly appreciated. Thanks in advance.


Here is the solution.All you need to do is add the below code in cellForRowAtIndexPath.

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleLeftMargin,
                                     .FlexibleWidth,
                                     .FlexibleRightMargin,
                                     .FlexibleTopMargin,
                                     .FlexibleHeight,
                                     .FlexibleBottomMargin]




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK