Reputation:
I am having a lot of problems centering an image in my UITableViewCell
. I have spent countless hours trying all the solutions listed on stack overflow as well as countless other websites. My text will center, but for some reason my image won't center and there is a white line on the left side of the table that I can not get rid of either.(From what I have read in the iOS documentation, this is new in iOS 7, but people have gotten it to go away) I have set all the insets to zero both programmatically and via the inspector. I am using core data to retrieve the images, which is working fine, it is just my image won't center.
Here is my method for the tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:@"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
Along with that, I can't seem to get my label to go above the image, which is a whole other problem. But what I have done to try to fix this is make a custom cell subclass, which didn't work. I am all out of resources and ideas to fix this.
EDIT ABOUT CUSTOM SUBCLASS
So I might have the wrong idea of a custom subclass of UITableViewCell
. But what I did was create a class, with two variables, one for the UIImageView
and the other for the label, in a class called customCell. Then in the main table view I called them like this:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
//cell.textLabel.text = [NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]];
//UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:@"image"]];
//cell.imageView.image = cellImage;
//cell.textLabel.textAlignment = NSTextAlignmentCenter;
//cell.textLabel.textColor = [UIColor whiteColor];
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]]];
[customCell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:@"image"]]];
return cell;
}
Then simply in the custom cell class I just connected the label and image to an IBOutlet and synthesized them.
EDIT 2 CUSTOM SUBCLASS ADDITION
The header file has two properties. One for cellTitle which is a UILabel and one for cellImage which is a UIImageView.
@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *cellImage;
@property (weak, nonatomic) IBOutlet UILabel *cellTitle;
The implementation file:
Here I am just synthesizing the two objects.
@synthesize cellImage;
@synthesize cellTitle;
- (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
}
Upvotes: 1
Views: 180
Reputation: 306
you can make custom cell for table view and load this cell in table view as you can place your contents in custom cell at desired position whatever you want.This is the best approach to make table view cell if your application requires complex design in table view.Example:static NSString *CustomCellIdentifier = @"CEll";//[NSString stringWithFormat:@"Cell%d",indexPath.row];
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; implement this in your cellforrowAtIndexPath method and add your cell nib -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) {
self = (customCell *)[[[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil] firstObject];}
}
Upvotes: 0
Reputation: 1156
DONT USE THE DEFAULT IMAGEVIEW FROM THE CELL... create a UIImageView... add it to cell's view at the desired position.. =) GL HF
Upvotes: 0
Reputation:
After a long day of trying to figure this out, I had one little mistake that caused this whole debacle. I had my cell Identifier set to "CellIdentifier" in the inspector. I changed it to "Cell" and edited some code and boom!
I added some code to the CustomCell:
-(void)layoutSubviews {
self.imageView.center = self.contentView.center;
[super layoutSubviews];
}
Which fixed everything. No more bar on the right side and both title and image are centered. Here is the final code in the TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
/*cell.textLabel.text = [NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:@"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];*/
[cell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:@"image"]]];
[cell.cellTitle setText:[NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]]];
return cell;
}
I still have commented out code in there, sorry about that. But thank you to all whole helped out in solving this problem!
Upvotes: 0
Reputation: 4244
Upvotes: 0
Reputation: 2151
To your cell subclass make a method
- (void)layoutSubviews {
self.cellTitle.frame = CGRectMake(...); //self.view.frame will return the cell's frame
self.cellImage.frame = CGRectMake(...); //you should set the frames of title and imageView however you want them in here
}
Upvotes: 0
Reputation: 227
CustomCell *customCell = (CustomCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:@"%@",
[moment valueForKey:@"name"]]];
[customCell.cellImage setImage:
[UIImage imageWithData:[moment valueForKey:@"image"]]];
return cell;
I believe you're returning the default cell
, not the customCell
Upvotes: 1