IOS - Excel 표 제작
보기: HJExcelView. h
//
// HJExcelView.h
// mehr
//
// Created by on 14-6-9.
// Copyright (c) 2014 Hjsj. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "HJExcelViewCell.h"
@protocol HJExcelViewViewDelegate, HJExcelViewViewDataSource;
@interface HJExcelView : UIView <HJExcelViewViewCellDelegate>
/** */
@property (nonatomic, assign) id <HJExcelViewViewDataSource> dataSource;
/** */
@property (nonatomic, assign) id <HJExcelViewViewDelegate> delegate;
/** */
@property (nonatomic, strong) UIColor *borderColor;
/** (>0 , 1.0)*/
@property (nonatomic) CGFloat borderWidth;
/** cell */
@property (nonatomic) CGFloat cellToBordeSpace;
/**
*
*
* @return void
*/
- (void)reloadData;
@end
@protocol HJExcelViewViewDelegate <NSObject>
@optional
/**
* cell
*
* @param excelView HJExcelView
* @param indexPath
*
* @return void
*/
- (void)excelView:(HJExcelView *)excelView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
/**
* cell , 40, borderWidth/2
*
* @param excelView HJExcelView
* @param row
*
* @return CGFloat
*/
- (CGFloat)excelView:(HJExcelView *)excelView heightForRow:(NSInteger)row;
/**
* cell , :60, borderWidth/2
*
* @param excelView HJExcelView
* @param section
*
* @return CGFloat
*/
- (CGFloat)excelView:(HJExcelView *)excelView widthInSection:(NSInteger)section;
@end
@protocol HJExcelViewViewDataSource <NSObject>
@required
/**
*
*
* @param excelView HJExcelView
*
* @return NSInteger
*/
- (NSInteger)numberOfRowsInExcelView:(HJExcelView *)excelView;
/**
*
*
* @param excelView HJExcelView
*
* @return NSInteger
*/
- (NSInteger)numberOfSectionsInExcelView:(HJExcelView *)excelView;
/**
* cell
*
* @param cell HJExcelViewCell
* @param indexPath
*
* @return HJExcelViewCell
*/
- (HJExcelViewCell *)excelViewCell:(HJExcelViewCell *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@end
구현: HJExcelView. m
//
// HJExcelView.m
// mehr
//
// Created by on 14-6-9.
// Copyright (c) 2014 Hjsj. All rights reserved.
//
#import "HJExcelView.h"
#import "HJExcelViewPoint.h"
@interface HJExcelView ()
{
@private
/** */
UIView *_boardView;
/** */
UIView *_contentView;
/** */
NSMutableArray *_cellRowArray, *_cellSectionArray;
}
@end
@implementation HJExcelView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_boardView = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:_boardView];
//
//
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[_boardView addGestureRecognizer:panRecognizer];
}
return self;
}
#pragma mark -
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
//
CGPoint translatedPoint = [recognizer translationInView:_boardView];
//
CGFloat x = recognizer.view.center.x + translatedPoint.x;
CGFloat y = recognizer.view.center.y + translatedPoint.y;
//
CGFloat contentWidth = _contentView.frame.size.width / 2;
CGFloat contentHeight = _contentView.frame.size.height / 2;
CGFloat mainWidth = self.frame.size.width / 2;
CGFloat mainHeight = self.frame.size.height / 2;
//
if (translatedPoint.y < 0)
{
//
if (contentHeight < mainHeight)
{
//
y = y > contentHeight ? y : contentHeight;
}
}
//
else
{
//
if (contentHeight < mainHeight)
{
//
y = y < self.frame.size.height - contentHeight ? y : self.frame.size.height - contentHeight;
}
}
//
if (translatedPoint.x < 0)
{
//
if (contentWidth < mainWidth)
{
//
x = x > contentWidth ? x : contentWidth;
}
}
//
else
{
if (contentWidth < mainWidth)
{
//
x = x < self.frame.size.width - contentWidth ? x : self.frame.size.width - contentWidth;
}
}
//
recognizer.view.center = CGPointMake(x, y);
//
[recognizer setTranslation:CGPointMake(0, 0) inView:_boardView];
}
#pragma mark -
- (UIColor *)borderColor
{
//
_borderColor = _borderColor ? _borderColor : [UIColor colorWithWhite:0.821 alpha:1.000];
return _borderColor;
}
#pragma mark
- (CGFloat)borderWidth
{
//
_borderWidth = _borderWidth > 0 ? _borderWidth : 1.0;
return _borderWidth;
}
#pragma mark -
- (void)reloadData
{
//
if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInExcelView:)] && [self.dataSource respondsToSelector:@selector(numberOfRowsInExcelView:)] && [self.dataSource respondsToSelector:@selector(excelViewCell:cellForRowAtIndexPath:)])
{
//
[_contentView removeFromSuperview];
_contentView = [[UIView alloc] initWithFrame:self.frame];
[_boardView addSubview:_contentView];
//
NSInteger rows = [self.dataSource numberOfRowsInExcelView:self];
//
NSInteger sections = [self.dataSource numberOfSectionsInExcelView:self];
CGFloat borderWidth = self.borderWidth / 2;
// cell
CGRect frame = CGRectMake(borderWidth + self.cellToBordeSpace * 2, borderWidth + self.cellToBordeSpace * 2, 0, 0);
_cellRowArray = [NSMutableArray array];
_cellSectionArray = [NSMutableArray array];
NSInteger sumHeight = 0, sumWidth = 0;
//
for (int row = 0; row < rows; row ++ )
{
//
frame.size.height = [self heightForRow:row];
//
HJExcelViewPoint *point = [HJExcelViewPoint excelViewPointWithX:frame.origin.x y:frame.origin.y];
[_cellRowArray addObject:point];
//
for (int section = 0; section < sections; section ++ )
{
//
frame.size.width = [self widthInSection:section];
//
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self addExcelViewCellWithIndexPath:indexPath frame:frame];
if (row == 0)
{
//
HJExcelViewPoint *point = [HJExcelViewPoint excelViewPointWithX:frame.origin.x y:frame.origin.y];
[_cellSectionArray addObject:point];
}
//
frame.origin.x += frame.size.width + self.cellToBordeSpace * 2;
}
// 0
frame.origin.y += frame.size.height + self.cellToBordeSpace * 2;
// ,
if (row == rows - 1)
{
sumHeight = frame.origin.y;
sumWidth = frame.origin.x;
}
frame.origin.x = self.borderWidth / 2 + self.cellToBordeSpace * 2;
}
//
HJExcelViewPoint *point;
UIView *line;
sumHeight -= self.cellToBordeSpace * 2;
sumWidth -= self.cellToBordeSpace * 2;
//
for (int row = 0; row < rows; row ++ )
{
point = [_cellRowArray objectAtIndex:row];
line = [[UIView alloc] initWithFrame:CGRectMake(point.x - borderWidth - self.cellToBordeSpace, point.y - borderWidth - self.cellToBordeSpace, sumWidth, self.borderWidth)];
line.backgroundColor = self.borderColor;
[_contentView addSubview:line];
}
//
line = [[UIView alloc] initWithFrame:CGRectMake(point.x - borderWidth - self.cellToBordeSpace, sumHeight - borderWidth + self.cellToBordeSpace, sumWidth, self.borderWidth)];
line.backgroundColor = self.borderColor;
[_contentView addSubview:line];
//
for (int section = 0; section < sections; section ++ )
{
point = [_cellSectionArray objectAtIndex:section];
line = [[UIView alloc] initWithFrame:CGRectMake(point.x - borderWidth - self.cellToBordeSpace, point.y - borderWidth - self.cellToBordeSpace, self.borderWidth, sumHeight)];
line.backgroundColor = self.borderColor;
[_contentView addSubview:line];
}
//
line = [[UIView alloc] initWithFrame:CGRectMake(sumWidth - borderWidth + self.cellToBordeSpace, point.y - borderWidth - self.cellToBordeSpace, self.borderWidth, sumHeight)];
line.backgroundColor = self.borderColor;
[_contentView addSubview:line];
//
frame = _boardView.frame;
frame.origin.x = - self.frame.size.width / 2;
frame.origin.y = - self.frame.size.height / 2;
frame.size.height = self.borderWidth + self.cellToBordeSpace + sumHeight + self.frame.size.height;
frame.size.width = self.borderWidth + self.cellToBordeSpace + sumWidth + self.frame.size.width;
_boardView.frame = frame;
//
frame = _contentView.frame;
frame.origin.x = self.frame.size.width / 2;
frame.origin.y = self.frame.size.height / 2;
frame.size.height = self.borderWidth + self.cellToBordeSpace + sumHeight;
frame.size.width = self.borderWidth + self.cellToBordeSpace + sumWidth;
_contentView.frame = frame;
}
}
#pragma mark - cell
- (CGFloat)heightForRow:(NSInteger)row
{
if ([self.delegate respondsToSelector:@selector(excelView:heightForRow:)])
{
return [self.delegate excelView:self heightForRow:row];
}
else
{
return 44;
}
}
#pragma mark cell
- (CGFloat)widthInSection:(NSInteger)section
{
if ([self.delegate respondsToSelector:@selector(excelView:heightForRow:)])
{
return [self.delegate excelView:self widthInSection:section];
}
else
{
return 70;
}
}
#pragma mark - cell
- (void)addExcelViewCellWithIndexPath:(NSIndexPath *)indexPath frame:(CGRect)frame
{
// cell
HJExcelViewCell *cell = [[HJExcelViewCell alloc] initWithFrame:frame];
// cell
cell = [self.dataSource excelViewCell:cell cellForRowAtIndexPath:indexPath];
//
cell.frame = frame;
//
cell.indexPath = indexPath;
//
cell.excelViewDelegate = self;
//
[_contentView addSubview:cell];
}
#pragma mark - HJExcelViewViewCellDelegate
- (void)excelViewCell:(HJExcelViewCell *)excelViewCell didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//
if (excelViewCell.selected && [self.delegate respondsToSelector:@selector(excelView:didSelectRowAtIndexPath:)])
{
// ,
[self.delegate excelView:self didSelectRowAtIndexPath:excelViewCell.indexPath];
}
}
@end
각각 cell: HJExcelView Cell. h
//
// HJExcelViewCell.h
// mehr
//
// Created by on 14-6-10.
// Copyright (c) 2014 Hjsj. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol HJExcelViewViewCellDelegate;
@interface HJExcelViewCell : UIView
/** , no */
@property (nonatomic) BOOL selected;
// , HJExcelView ,
/** */
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, assign) id <HJExcelViewViewCellDelegate> excelViewDelegate;
@end
// HJExcelView ,
@protocol HJExcelViewViewCellDelegate <NSObject>
@optional
/**
* cell
*
* @param excelViewCell HJExcelViewCell
* @param indexPath
*
* @return void
*/
- (void)excelViewCell:(HJExcelViewCell *)excelViewCell didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
@end
구현: HJExcelViewCell. m
//
// HJExcelViewCell.m
// mehr
//
// Created by on 14-6-10.
// Copyright (c) 2014 Hjsj. All rights reserved.
//
#import "HJExcelViewCell.h"
@implementation HJExcelViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.excelViewDelegate excelViewCell:self didSelectRowAtIndexPath:self.indexPath];
}
@end
위치: HJExcelView Point. h
//
// HJExcelViewPoint.h
// mehr
//
// Created by on 14-6-10.
// Copyright (c) 2014 Hjsj. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HJExcelViewPoint : NSObject
/** x*/
@property (nonatomic) CGFloat x;
/** y*/
@property (nonatomic) CGFloat y;
/**
*
*
* @param x x
* @param y y
*
* @return HJExcelViewPoint
*/
+ (id)excelViewPointWithX:(CGFloat)x y:(CGFloat)y;
@end
구현: HJExcelViewpoint. m
//
// HJExcelViewPoint.m
// mehr
//
// Created by on 14-6-10.
// Copyright (c) 2014 Hjsj. All rights reserved.
//
#import "HJExcelViewPoint.h"
@implementation HJExcelViewPoint
+ (id)excelViewPointWithX:(CGFloat)x y:(CGFloat)y
{
HJExcelViewPoint *point = [[[self class] alloc] init];
point.x = x;
point.y = y;
return point;
}
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.