Permalink
Browse files

Refactor table cells.

  • Loading branch information...
kishikawakatsumi committed Aug 29, 2009
1 parent 4eb4eeb commit 083d7794a60c5ea2bd80cfed7b4680607261576f
View
@@ -1,13 +1,15 @@
#import <UIKit/UIKit.h>
@interface DiaryListCell : UITableViewCell {
- IBOutlet UILabel *titleLabel;
- IBOutlet UILabel *dateLabel;
- IBOutlet UILabel *numberLabel;
+ NSString *titleText;
+ NSString *dateText;
+ NSString *numberText;
}
-@property (nonatomic, retain) UILabel *titleLabel;
-@property (nonatomic, retain) UILabel *dateLabel;
-@property (nonatomic, retain) UILabel *numberLabel;
+@property (nonatomic, retain) NSString *titleText;
+@property (nonatomic, retain) NSString *dateText;
+@property (nonatomic, retain) NSString *numberText;
+
+- (void)drawSelectedBackgroundRect:(CGRect)rect;
@end
View
@@ -1,51 +1,90 @@
#import "DiaryListCell.h"
+#import "DiaryListCellSelectedBackgroundView.h"
+#import "TableCellDrawing.h"
@implementation DiaryListCell
-@synthesize titleLabel;
-@synthesize dateLabel;
-@synthesize numberLabel;
+@synthesize titleText;
+@synthesize dateText;
+@synthesize numberText;
+
+static UIColor *blueColor = NULL;
+static UIColor *grayColor = NULL;
+static UIColor *darkGrayColor = NULL;
+
++ (void)initialize {
+ blueColor = [[UIColor colorWithRed:0.0f green:0.2f blue:1.0f alpha:1.0f] retain];
+ grayColor = [[UIColor colorWithRed:0.4f green:0.4f blue:0.4f alpha:1.0f] retain];
+ darkGrayColor = [[UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f] retain];
+}
+
+- (void)setTitleText:(NSString *)text {
+ if (titleText != text) {
+ [titleText release];
+ titleText = [text retain];
+ [self setNeedsDisplay];
+ }
+}
+
+- (void)setDateText:(NSString *)text {
+ if (dateText != text) {
+ [dateText release];
+ dateText = [text retain];
+ [self setNeedsDisplay];
+ }
+}
+
+- (void)setNumberText:(NSString *)text {
+ if (numberText != text) {
+ [numberText release];
+ numberText = [text retain];
+ [self setNeedsDisplay];
+ }
+}
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
[self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[self setOpaque:YES];
- titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 3.0f, 277.0f, 37.0f)];
- [titleLabel setOpaque:YES];
- [titleLabel setNumberOfLines:2];
- [titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
- [titleLabel setTextColor:[UIColor colorWithRed:0.0f green:0.2f blue:1.0f alpha:1.0f]];
- [titleLabel setHighlightedTextColor:[UIColor whiteColor]];
- [self addSubview:titleLabel];
-
- dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 39.0f, 277.0f, 24.0f)];
- [dateLabel setOpaque:YES];
- [dateLabel setFont:[UIFont systemFontOfSize:12.0f]];
- [dateLabel setTextAlignment:UITextAlignmentRight];
- [dateLabel setTextColor:[UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];
- [dateLabel setHighlightedTextColor:[UIColor whiteColor]];
- [self addSubview:dateLabel];
-
- numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 16.0f, 21.0f)];
- [numberLabel setOpaque:YES];
- [numberLabel setFont:[UIFont systemFontOfSize:9.0f]];
- [numberLabel setTextAlignment:UITextAlignmentRight];
- [numberLabel setTextColor:[UIColor blackColor]];
- [numberLabel setHighlightedTextColor:[UIColor whiteColor]];
- [self addSubview:numberLabel];
+ DiaryListCellSelectedBackgroundView *selectedBackgroundView = [[DiaryListCellSelectedBackgroundView alloc] initWithFrame:[self frame]];
+ [selectedBackgroundView setCell:self];
+ [self setSelectedBackgroundView:selectedBackgroundView];
+ [selectedBackgroundView release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
+ [super setSelected:selected animated:animated];
+ [self setNeedsDisplay];
+ [self.selectedBackgroundView setNeedsDisplay];
+}
+
+- (void)drawRect:(CGRect)rect {
+ [super drawRect:rect];
+ [blueColor set];
+ [titleText drawInRect:CGRectMake(20.0f, 3.0f, 277.0f, 37.0f) withFont:[UIFont boldSystemFontOfSize:14.0f] lineBreakMode:UILineBreakModeTailTruncation];
+ [darkGrayColor set];
+ [dateText drawInRect:CGRectMake(20.0f, 42.0f, 277.0f, 24.0f) withFont:[UIFont systemFontOfSize:12.0f] lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentRight];
+ [[UIColor blackColor] set];
+ [numberText drawInRect:CGRectMake(0.0f, 26.0f, 16.0f, 21.0f) withFont:[UIFont systemFontOfSize:9.0f] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
+}
+
+- (void)drawSelectedBackgroundRect:(CGRect)rect {
+ CGGradientRef gradientForSelected = createTwoColorsGradient(5, 140, 245, 1, 93, 230);
+ drawRoundedRectBackgroundGradient(rect, gradientForSelected, NO, NO, NO);
+ CGGradientRelease(gradientForSelected);
+ [[UIColor whiteColor] set];
+ [titleText drawInRect:CGRectMake(20.0f, 3.0f, 277.0f, 37.0f) withFont:[UIFont boldSystemFontOfSize:14.0f] lineBreakMode:UILineBreakModeTailTruncation];
+ [dateText drawInRect:CGRectMake(20.0f, 42.0f, 277.0f, 24.0f) withFont:[UIFont systemFontOfSize:12.0f] lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentRight];
+ [numberText drawInRect:CGRectMake(0.0f, 26.0f, 16.0f, 21.0f) withFont:[UIFont systemFontOfSize:9.0f] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
}
- (void)dealloc {
- [titleLabel release];
- [dateLabel release];
- [numberLabel release];
+ [titleText release];
+ [dateText release];
+ [numberText release];
[super dealloc];
}
@@ -0,0 +1,19 @@
+//
+// DiaryListCellSelectedBackgroundView.h
+// HatenaTouch
+//
+// Created by KISHIKAWA Katsumi on 09/08/30.
+// Copyright 2009 KISHIKAWA Katsumi. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class DiaryListCell;
+
+@interface DiaryListCellSelectedBackgroundView : UIView {
+ DiaryListCell *cell;
+}
+
+@property (nonatomic, assign) DiaryListCell *cell;
+
+@end
@@ -0,0 +1,24 @@
+//
+// DiaryListCellSelectedBackgroundView.m
+// HatenaTouch
+//
+// Created by KISHIKAWA Katsumi on 09/08/30.
+// Copyright 2009 KISHIKAWA Katsumi. All rights reserved.
+//
+
+#import "DiaryListCellSelectedBackgroundView.h"
+#import "DiaryListCell.h"
+
+@implementation DiaryListCellSelectedBackgroundView
+
+@synthesize cell;
+
+- (void)drawRect:(CGRect)rect {
+ [cell drawSelectedBackgroundRect:rect];
+}
+
+- (void)dealloc {
+ [super dealloc];
+}
+
+@end
@@ -4,9 +4,10 @@
#import "HatenaAtomPub.h"
#import "DiaryListCell.h"
#import "DiaryViewController.h"
-#import "DiaryNextCellController.h"
#import "DiaryNextCell.h"
+#define ROWNUM_PER_PAGE 20
+
@implementation DiaryListViewController
@synthesize diaryListView;
@@ -15,8 +16,8 @@ @implementation DiaryListViewController
@synthesize forceReload;
- (void)dealloc {
- [dateFormatter2 release];
[dateFormatter1 release];
+ [dateFormatter2 release];
[diaryList release];
[diaryListView setDelegate:nil];
[diaryListView release];
@@ -29,7 +30,7 @@ - (BOOL)isDraft {
- (void)_loadDiaryList {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
HatenaAtomPub *atomPub = [[HatenaAtomPub alloc] init];
NSData *data = [atomPub requestBlogCollectionWhetherDraft:draft pageNumber:page];
@@ -40,7 +41,7 @@ - (void)_loadDiaryList {
[parser release];
[atomPub release];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[pool release];
}
@@ -50,7 +51,7 @@ - (void)loadDiaryList {
- (void)addDiaryList:(id)entry {
if (!diaryList) {
- diaryList = [[NSMutableArray alloc] initWithCapacity:20];
+ diaryList = [[NSMutableArray alloc] initWithCapacity:ROWNUM_PER_PAGE];
}
[diaryList addObject:entry];
[diaryListView reloadData];
@@ -66,10 +67,8 @@ - (void)refleshIfNeeded {
}
- (void)loadNext {
- [diaryList release];
- diaryList = nil;
page += 1;
- [self refleshIfNeeded];
+ [self loadDiaryList];
}
- (BOOL)deleteEntry:(NSDictionary *)entry {
@@ -94,20 +93,16 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
NSInteger count = [diaryList count];
if (count == 0) {
return 0;
- } else if (count == 20) {
- return count + 1;
} else {
- return count;
+ return count + 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [diaryList count]) {
DiaryNextCell *cell = (DiaryNextCell *)[tableView dequeueReusableCellWithIdentifier:@"DiaryNextCell"];
if (cell == nil) {
- DiaryNextCellController *controller = [[DiaryNextCellController alloc] initWithNibName:@"DiaryNextCell" bundle:nil];
- cell = (DiaryNextCell *)controller.view;
- [controller release];
+ cell = [[[DiaryNextCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 64.0f) reuseIdentifier:@"DiaryNextCell"] autorelease];
}
return cell;
@@ -118,10 +113,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
NSDictionary *entry = [diaryList objectAtIndex:indexPath.row];
- [cell.titleLabel setText:[entry objectForKey:@"title"]];
+ [cell setTitleText:[entry objectForKey:@"title"]];
NSDate *date = [dateFormatter1 dateFromString:[entry objectForKey:@"published"]];
- [cell.dateLabel setText:[dateFormatter2 stringFromDate:date]];
- [cell.numberLabel setText:[NSString stringWithFormat:@"%d", (20 * (page - 1)) + indexPath.row + 1]];
+ [cell setDateText:[dateFormatter2 stringFromDate:date]];
+ [cell setNumberText:[NSString stringWithFormat:@"%d", (ROWNUM_PER_PAGE * (page <= 1 ? 0 : page - 2)) + indexPath.row + 1]];
return cell;
}
View
@@ -2,4 +2,6 @@
@interface DiaryNextCell : UITableViewCell
+- (void)drawSelectedBackgroundRect:(CGRect)rect;
+
@end
View
@@ -1,9 +1,46 @@
#import "DiaryNextCell.h"
+#import "DiaryNextCellSelectedBackgroundView.h"
+#import "TableCellDrawing.h"
@implementation DiaryNextCell
+static NSString *cellText;
+
++ (void)initialize {
+ cellText = NSLocalizedString(@"Read Next 20 Entries...", nil);
+}
+
+- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
+ if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
+ [self setAccessoryType:UITableViewCellAccessoryNone];
+ [self setOpaque:YES];
+
+ DiaryNextCellSelectedBackgroundView *selectedBackgroundView = [[DiaryNextCellSelectedBackgroundView alloc] initWithFrame:[self frame]];
+ [selectedBackgroundView setCell:self];
+ [self setSelectedBackgroundView:selectedBackgroundView];
+ [selectedBackgroundView release];
+ }
+ return self;
+}
+
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
+ [self setNeedsDisplay];
+ [self.selectedBackgroundView setNeedsDisplay];
+}
+
+- (void)drawRect:(CGRect)rect {
+ [super drawRect:rect];
+ [[UIColor blackColor] set];
+ [cellText drawInRect:CGRectMake(20.0f, 24.0f, 280.0f, 21.0f) withFont:[UIFont boldSystemFontOfSize:17.0f] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
+}
+
+- (void)drawSelectedBackgroundRect:(CGRect)rect {
+ CGGradientRef gradientForSelected = createTwoColorsGradient(5, 140, 245, 1, 93, 230);
+ drawRoundedRectBackgroundGradient(rect, gradientForSelected, NO, NO, NO);
+ CGGradientRelease(gradientForSelected);
+ [[UIColor whiteColor] set];
+ [cellText drawInRect:CGRectMake(20.0f, 24.0f, 280.0f, 21.0f) withFont:[UIFont boldSystemFontOfSize:17.0f] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
}
- (void)dealloc {
@@ -0,0 +1,19 @@
+//
+// DiaryNextCellSelectedBackgroundView.h
+// HatenaTouch
+//
+// Created by KISHIKAWA Katsumi on 09/08/30.
+// Copyright 2009 KISHIKAWA Katsumi. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class DiaryNextCell;
+
+@interface DiaryNextCellSelectedBackgroundView : UIView {
+ DiaryNextCell *cell;
+}
+
+@property (nonatomic, assign) DiaryNextCell *cell;
+
+@end
@@ -0,0 +1,24 @@
+//
+// DiaryNextCellSelectedBackgroundView.m
+// HatenaTouch
+//
+// Created by KISHIKAWA Katsumi on 09/08/30.
+// Copyright 2009 KISHIKAWA Katsumi. All rights reserved.
+//
+
+#import "DiaryNextCellSelectedBackgroundView.h"
+#import "DiaryNextCell.h"
+
+@implementation DiaryNextCellSelectedBackgroundView
+
+@synthesize cell;
+
+- (void)drawRect:(CGRect)rect {
+ [cell drawSelectedBackgroundRect:rect];
+}
+
+- (void)dealloc {
+ [super dealloc];
+}
+
+@end
Oops, something went wrong.

0 comments on commit 083d779

Please sign in to comment.