Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kyle Yoon
3,725 PointsBlog Reader - How to get rid of left indent?
When I run my Blog Reader app, the images and and content in a cell have a left indent. On the other hand Amit's Blog Reader app aligns to the left of the screen. I would post a screenshot but I dont know how (so I guess that is my second question). Please help!
3 Answers
Thomas Nilsen
14,957 Pointsupload the image to www.imgur.com and post the link here.
Stone Preston
42,016 PointsI answered this a while back. basically I tried changing the default table view cell image views frame around and couldnt really get it to work. What I ultimately ended up doing was just subclass UITableViewCell and design the cell with the imageView right on the edge of the content view.
First remove the current prototype cell by selecting it in your storyboard and then pressing delete.
After you delete it, search for cell in the object library and then drag it on to your tableView near the top.
Then drag an image view on to that cell and size it so that its as tall as the cell and square. then move it over so that its right on the edge.
add a title label and size it so that it extends all the way to the right side of the table view then under that add a subtitle label that has a smaller font size and a grey color and also extends to the far side of the tableView.
you can set the cell identifier to whatever you want in the attributes inspect, I named mine "customCell"
then create a new class called THCustomCell and make it a subclass of UITableViewCell. After creating it set the class of your prototype cell to be THCustomCell using the identity inspector.
after setting the class of your cell open the assistant editor and control click and drag from the image view to the header file of your new class, call the outlet myImageView (if you call it imageView it messes things up since the superclass has a property named imageView as well so name it something else)
then control click and drag from the title label to the header, call it titleLabel
then control click and drag from the subtitleLabel to the header, call it subtitleLabel
this is what my header looked like after connecting the outlet properties:
#import <UIKit/UIKit.h>
@interface THCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *subtitleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
@end
after that switch to your viewController file. import your new custom cell class
#import "THCustomCell.h"
then in cellForRowAtIndexPath you need to set the cell identifier to whatever you named it and instantiate the cell using your new class
//in cellForRowAtIndexPath
//use the identifier you gave it in your storyboard
static NSString *CellIdentifier = @"customCell";
//use your new custom cell class
THCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
then configure the cell
// Configure the cell...
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
//set the imageView property of your custom cell
if ( [blogPost.thumbnail isKindOfClass:[NSString class]]) {
NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.myImageView.image = image;
} else {
cell.myImageView.image = [UIImage imageNamed:@"treehouse"];
}
//set the title label of your custom cell
cell.titleLabel.text = blogPost.title;
//set the subtitleLabel of your custom cell
cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ - %@", blogPost.author, [blogPost formattedDate]];
return cell;
below is my full implementation of the cellForRowAtIndex path method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
SPCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
if ( [blogPost.thumbnail isKindOfClass:[NSString class]]) {
NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.myImageView.image = image;
} else {
cell.myImageView.image = [UIImage imageNamed:@"treehouse"];
}
cell.titleLabel.text = blogPost.title;
cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ - %@", blogPost.author, [blogPost formattedDate]];
return cell;
}
you also may have to recreate your segue from the cell to the web view if it got deleted when you removed the old prototype.
a lot of steps but not too bad
Kyle Yoon
3,725 PointsWow, thanks for the fast answers!
Stone Preston
42,016 Pointsno problem. I was lucky I remembered some keywords from that post title and was able to find it pretty quickly