Add background image style provider

This commit is contained in:
Roman Efimov
2013-03-01 16:39:18 -06:00
parent 39ef9bb7f9
commit 5ef4dd4b30
9 changed files with 161 additions and 7 deletions

View File

@@ -27,9 +27,9 @@
@implementation RETableViewBoolCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
_switch = [[UISwitch alloc] init];

View File

@@ -27,9 +27,9 @@
@implementation RETableViewStringCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager];
if (self) {
// Initialization code
}

View File

@@ -27,9 +27,9 @@
@implementation RETableViewTextCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textLabel.backgroundColor = [UIColor clearColor];

View File

@@ -26,14 +26,19 @@
#import <UIKit/UIKit.h>
#import "RETableViewSection.h"
@class RETableViewManager;
@interface RETableViewCell : UITableViewCell
@property (strong, nonatomic) UIImageView *backgroundImageView;
@property (assign, nonatomic) NSInteger row;
@property (assign, nonatomic) NSInteger sectionIndex;
@property (weak, nonatomic) UITableView *parentTableView;
@property (weak, nonatomic) RETableViewManager *tableViewManager;
@property (weak, nonatomic) RETableViewSection *section;
@property (strong, nonatomic) NSObject *item;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager;
- (void)prepare;
+ (CGFloat)height;

View File

@@ -24,9 +24,30 @@
//
#import "RETableViewCell.h"
#import "RETableViewManager.h"
@implementation RETableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier tableViewManager:(RETableViewManager *)tableViewManager
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.tableViewManager = tableViewManager;
if ([self hasCustomBackgroundImage]) {
self.backgroundView = [[UIView alloc] initWithFrame:self.contentView.bounds];
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_backgroundImageView = [[UIImageView alloc] init];
[self.backgroundView addSubview:_backgroundImageView];
}
}
return self;
}
- (BOOL)hasCustomBackgroundImage
{
return [self.tableViewManager.style backgroundImageForCellType:RETableViewCellFirst] || [self.tableViewManager.style backgroundImageForCellType:RETableViewCellMiddle] || [self.tableViewManager.style backgroundImageForCellType:RETableViewCellLast] || [self.tableViewManager.style backgroundImageForCellType:RETableViewCellSingle];
}
+ (CGFloat)height
{
return 44;
@@ -37,4 +58,29 @@
}
- (void)layoutSubviews
{
[super layoutSubviews];
if ([self hasCustomBackgroundImage]) {
if (self.row == 0 && self.section.items.count == 1) {
_backgroundImageView.image = [self.tableViewManager.style backgroundImageForCellType:RETableViewCellSingle];
}
if (self.row == 0 && self.section.items.count > 1) {
_backgroundImageView.image = [self.tableViewManager.style backgroundImageForCellType:RETableViewCellFirst];
}
if (self.row > 0 && self.row < self.section.items.count - 1 && self.section.items.count > 2) {
_backgroundImageView.image = [self.tableViewManager.style backgroundImageForCellType:RETableViewCellMiddle];
}
if (self.row == self.section.items.count - 1 && self.section.items.count > 1) {
_backgroundImageView.image = [self.tableViewManager.style backgroundImageForCellType:RETableViewCellLast];
}
_backgroundImageView.frame = CGRectMake(0, 0, _backgroundImageView.image.size.width, _backgroundImageView.image.size.height);
}
}
@end

View File

@@ -0,0 +1,48 @@
//
// RETableViewCellStyle.h
// RETableViewManager
//
// Copyright (c) 2013 Roman Efimov (https://github.com/romaonthego)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
typedef enum _RETableViewCellType {
RETableViewCellFirst,
RETableViewCellMiddle,
RETableViewCellLast,
RETableViewCellSingle
} RETableViewCellType;
@interface RETableViewCellStyle : NSObject {
NSMutableDictionary *_backgroundImages;
}
@property (strong, nonatomic) UIFont *font;
- (UIImage *)backgroundImageForCellType:(RETableViewCellType)cellType;
- (void)setBackgroundImage:(UIImage *)image forCellType:(RETableViewCellType)cellType;
/*@property (strong, nonatomic) UIImage *backgroundImageFirst;
@property (strong, nonatomic) UIImage *backgroundImageMiddle;
@property (strong, nonatomic) UIImage *backgroundImageLast;
@property (strong, nonatomic) UIImage *backgroundImageSingle;*/
@end

View File

@@ -0,0 +1,52 @@
//
// RETableViewCellStyle.m
// RETableViewManager
//
// Copyright (c) 2013 Roman Efimov (https://github.com/romaonthego)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "RETableViewCellStyle.h"
@implementation RETableViewCellStyle
- (id)init
{
self = [super init];
if (!self)
return nil;
_backgroundImages = [[NSMutableDictionary alloc] init];
return self;
}
- (UIImage *)backgroundImageForCellType:(RETableViewCellType)cellType
{
return [_backgroundImages objectForKey:@(cellType)];
}
- (void)setBackgroundImage:(UIImage *)image forCellType:(RETableViewCellType)cellType
{
if (image)
[_backgroundImages setObject:image forKey:@(cellType)];
}
@end

View File

@@ -25,6 +25,7 @@
#import <UIKit/UIKit.h>
#import "RETableViewSection.h"
#import "RETableViewCellStyle.h"
#import "RETableViewCell.h"
#import "RETableViewStringCell.h"
@@ -37,6 +38,7 @@
@property (strong, nonatomic) NSMutableArray *sections;
@property (strong, nonatomic) NSMutableDictionary *mapping;
@property (strong, nonatomic) RETableViewCellStyle *style;
@property (assign, nonatomic) id<RETableViewManagerDelegate>delegate;
- (void)addSection:(RETableViewSection *)section;

View File

@@ -35,6 +35,7 @@
_sections = [[NSMutableArray alloc] init];
_mapping = [[NSMutableDictionary alloc] init];
_style = [[RETableViewCellStyle alloc] init];
[self setDefaultMapping];
@@ -97,7 +98,7 @@
RETableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier tableViewManager:self];
}
cell.row = indexPath.row;