您的位置:首页 > 其它

创建表格视图

2016-06-24 14:27 267 查看
创建表格方法,demo git地址

介绍: 本代码提供两种方式,让开发者更方便地创建一个表格视图,第一种方式是通过代理的方法实现整个视图的搭建,仿造UITableView模式进行编写,其功能强大;第二种方式是对第一种方式的一种封装,让开发者能更快速地实现视图的搭建,但对于过于个性的表格,建议采用第一种方式。

KWFormViewQuickBuilder.h
#import <Foundation/Foundation.h>
#import "KWFormView.h"

extern NSString *const noAction;
extern NSString *const noColor;

@interface KWFormViewQuickBuilder : NSObject
- (KWFormView *)startCreatWithWidths:(NSArray *)widths startPoint:(CGPoint)point;

- (void)setTextColor:(UIColor *)tcolor backgroundColor:(UIColor *)bcolor forRow:(NSInteger)row;

- (void)setBorderColor:(UIColor *)color;

- (void)setActionTarget:(id)target;

- (void)addRecord:(NSArray *)record SELNames:(NSArray *)SELs;

- (void)addRecord:(NSArray *)record;

- (NSString *)valueAtcolumn:(NSInteger)column InRow:(NSInteger)row;
@end

KWFromView

#import <UIKit/UIKit.h>
@class KWFormView;

typedef NS_ENUM(NSInteger, KWFormViewMode)
{
KWFormViewModeDefault,
KWFormViewModeLeft,
KWFormViewModeRight,
KWFormViewModeMiddle,
};

@protocol KWFormViewDataSource <NSObject>
- (NSInteger)numberOfRowsInFormView:(KWFormView *)formView;
- (NSInteger)formViewColumnsInRow:(KWFormView *)formView;
- (CGFloat)formView:(KWFormView *)formView widthForColumn:(NSInteger)column;
- (NSString *)formView:(KWFormView *)formView textForColumn:(NSInteger)column inRow:(NSInteger)row;
@optional
- (CGFloat)formView:(KWFormView *)formView heightForRow:(NSInteger)row;
- (UIColor *)formView:(KWFormView *)formView colorOfColumn:(NSInteger)column inRow:(NSInteger)row;
- (UIColor *)formView:(KWFormView *)formView contentColorOfColumn:(NSInteger)column inRow:(NSInteger)row;
- (UIColor *)formViewBorderColor:(KWFormView *)formView;
- (UIView *)formViewViewForFooter:(KWFormView *)formView;
- (UIFont *)formViewFontOfContent:(KWFormView *)formView;
- (BOOL)formView:(KWFormView *)formView addActionForColumn:(NSInteger)column inRow:(NSInteger)row;
@end

@protocol KWFormViewDelegate <NSObject>
@optional
- (void)formView:(KWFormView *)formView didSelectColumn:(NSInteger)column inRow:(NSInteger)row;
@end

@interface KWFormView : UIView
@property (nonatomic, weak) id<KWFormViewDataSource> dataSource;
@property (nonatomic, weak) id<KWFormViewDelegate> delegate;
@property (nonatomic, assign) BOOL thickBorder;
@property (nonatomic, assign ,readonly) KWFormViewMode mode;
- (void)setMode:(KWFormViewMode)mode withMargin:(CGFloat)margin;
- (UIButton *)itemAtColumn:(NSInteger)column inRow:(NSInteger)row;
@end

@interface UIImage (KWFormView)
- (BOOL)hasAlpha;
- (UIImage *)imageWithAlpha;
- (UIImage *)transparentBorderImage:(NSUInteger)borderSize;
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha;
+ (UIImage *)imageFromColor:(UIColor *)color;
@end


其中UIImage(KWFromView)是对UIImage的拓展,用于个性化生成图片
@interface UIImage ()
- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size;
@end

@implementation UIImage (Alpha)

- (BOOL)hasAlpha
{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(self.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}

- (UIImage *)imageWithAlpha
{
if ([self hasAlpha]) {
return self;
}

CGImageRef imageRef = self.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);

// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error
CGContextRef offscreenContext = CGBitmapContextCreate(NULL,
width,
height,
8,
0,
CGImageGetColorSpace(imageRef),
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

// Draw the image into the context and retrieve the new image, which will now have an alpha layer
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(offscreenContext);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];

// Clean up
CGContextRelease(offscreenContext);
CGImageRelease(imageRefWithAlpha);

return imageWithAlpha;
}

- (UIImage *)transparentBorderImage:(NSUInteger)borderSize
{
// If the image does not have an alpha layer, add one
UIImage *image = [self imageWithAlpha];

CGRect newRect = CGRectMake(0, 0, image.size.width + borderSize * 2, image.size.height + borderSize * 2);

// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(self.CGImage),
0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));

// Draw the image in the center of the context, leaving a gap around the edges
CGRect imageLocation = CGRectMake(borderSize, borderSize, image.size.width, image.size.height);
CGContextDrawImage(bitmap, imageLocation, self.CGImage);
CGImageRef borderImageRef = CGBitmapContextCreateImage(bitmap);

// Create a mask to make the border transparent, and combine it with the image
CGImageRef maskImageRef = [self newBorderMask:borderSize size:newRect.size];
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(borderImageRef, maskImageRef);
UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];

// Clean up
CGContextRelease(bitmap);
CGImageRelease(borderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);

return transparentBorderImage;
}

#pragma mark - Private helper methods

- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

// Build a context that's the same dimensions as the new size
CGContextRef maskContext = CGBitmapContextCreate(NULL,
size.width,
size.height,
8, // 8-bit grayscale
0,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaNone);

// Start with a mask that's entirely transparent
CGContextSetFillColorWithColor(maskContext, [UIColor blackColor].CGColor);
CGContextFillRect(maskContext, CGRectMake(0, 0, size.width, size.height));

// Make the inner part (within the border) opaque
CGContextSetFillColorWithColor(maskContext, [UIColor whiteColor].CGColor);
CGContextFillRect(maskContext, CGRectMake(borderSize, borderSize, size.width - borderSize * 2, size.height - borderSize * 2));

// Get an image of the context
CGImageRef maskImageRef = CGBitmapContextCreateImage(maskContext);

// Clean up
CGContextRelease(maskContext);
CGColorSpaceRelease(colorSpace);

return maskImageRef;
}

- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);

CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);

CGContextSetBlendMode(ctx, kCGBlendModeMultiply);

CGContextSetAlpha(ctx, alpha);

CGContextDrawImage(ctx, area, self.CGImage);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;
}

+ (UIImage *)imageFromColor:(UIColor *)color
{
CGSize size = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, (CGRect){CGPointZero,size});
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: