Skip to content

Instantly share code, notes, and snippets.

@thanhtungka91
Last active December 12, 2017 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanhtungka91/b1a6562e3b78d784d43385fa9d6a7f86 to your computer and use it in GitHub Desktop.
Save thanhtungka91/b1a6562e3b78d784d43385fa9d6a7f86 to your computer and use it in GitHub Desktop.
simple customize table
#define CELL_HEIGHT 95.0f
@interface RequestListViewController ()
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) IBOutlet UILabel *titleLabel;
@property (nonatomic, weak) IBOutlet UIButton *backButton;
@property (nonatomic, strong) NSArray *requestLists;
@property (nonatomic, strong) NSArray *sectionTitles;
@end
@implementation RequestListViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
self.tableView.contentInset = UIEdgeInsetsMake(44.0f, 0, 0, 0);
} else {
self.tableView.contentInset = UIEdgeInsetsMake(-self.tableView.tableHeaderView.frame.size.height, 0, 0, 0);
}
_isBackButtonEnabled = YES;
self.selectHideRequests = [@[] mutableCopy];
}
return self;
}
- (void)setIsBackButtonEnabled:(BOOL)isBackButtonEnabled
{
_isBackButtonEnabled = isBackButtonEnabled;
self.backButton.hidden = !isBackButtonEnabled;
self.backButton.enabled = !self.backButton.hidden;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
self.backButton.hidden = !_isBackButtonEnabled;
self.backButton.enabled = !self.backButton.hidden;
self.titleLabel.text = self.title;
UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didRightSwipeCell:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.tableView addGestureRecognizer:swipeGesture];
swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didLeftSwipeCell:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:swipeGesture];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)didRightSwipeCell:(UISwipeGestureRecognizer*)swipeRecognizer
{
CGPoint loc = [swipeRecognizer locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:loc];
RequestListCell* cell = (RequestListCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if (self.hiddenRemoveRequestButton) {
return;
}
if (![self.selectHideRequests containsObject:@(cell.request.requestId)] ||
[RequestMark isMarkWithRequestId:cell.request.requestId] == NO ||
cell.request.requestStatusId == 1 || cell.request.requestStatusId == 3 || cell.request.requestStatusId == 4) {
return;
}
if (cell) {
[cell rightSwipeAnimation];
[self.selectHideRequests removeObject:@(cell.request.requestId)];
}
}
- (void)didLeftSwipeCell:(UISwipeGestureRecognizer*)swipeRecognizer
{
CGPoint loc = [swipeRecognizer locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:loc];
RequestListCell* cell = (RequestListCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if (self.hiddenRemoveRequestButton) {
return;
}
if ([self.selectHideRequests containsObject:@(cell.request.requestId)] ||
[RequestMark isMarkWithRequestId:cell.request.requestId] == NO ||
cell.request.requestStatusId == 1 || cell.request.requestStatusId == 3 || cell.request.requestStatusId == 4) {
return;
}
if (cell) {
[self allHiddenRemoveButton];
[cell leftSwipeAnimation];
[self.selectHideRequests addObject:@(cell.request.requestId)];
}
}
- (void)allHiddenRemoveButton
{
for (RequestListCell *cell in self.tableView.visibleCells) {
if ([self.selectHideRequests containsObject:@(cell.request.requestId)]) {
[cell rightSwipeAnimation];
}
}
[self.selectHideRequests removeAllObjects];
}
#pragma mark - IBAction
- (IBAction)tapBackButton:(id)sender
{
[Global playSound];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)reloadSearchOrder
{
if ([self.navigationController.viewControllers count] > 1) {
[self.navigationController popViewControllerAnimated:YES];
}
self.navigationItem.rightBarButtonItem = nil;
[self.tableView reloadData];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
} else {
return NO;
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.requestLists.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.requestLists.count > section) {
int numberofrow =[(NSArray*)self.requestLists[section] count];
return [(NSArray*)self.requestLists[section] count];
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (self.sectionTitles.count > section) {
return [Global escapeNullForString:self.sectionTitles[section]];
}
return @"";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RequestListCell *cell = (RequestListCell *) [tableView dequeueReusableCellWithIdentifier:@"RequestListCell"];
if (cell == nil) {
cell = (RequestListCell *) [[[NSBundle mainBundle] loadNibNamed:@"RequestListCell" owner:self options:nil] objectAtIndex:0];
}
if (self.requestLists.count > indexPath.section &&
[(NSArray*)self.requestLists[indexPath.section] count] > indexPath.row) {
[cell setRequest:self.requestLists[indexPath.section][indexPath.row]];
}
if ([self.selectHideRequests containsObject:@(cell.request.requestId)]) {
[cell swipe];
} else {
[cell swipeReset];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return CELL_HEIGHT;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.row % 2) == 0) {
cell.backgroundColor = [UIColor whiteColor];
} else {
[cell setBackgroundColor:[UIColor colorWithRed:0.89f green:0.92f blue:0.95 alpha:1.0f]];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.requestLists.count > indexPath.section &&
[(NSArray*)self.requestLists[indexPath.section] count] > indexPath.row) {
[self showDetailViewWithXMRequest:self.requestLists[indexPath.section][indexPath.row]];
}
[self allHiddenRemoveButton];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
return [Global getBaseViewForHeaderInSection:sectionTitle];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
scrollBeginingPoint = [scrollView contentOffset];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint currentPoint = [scrollView contentOffset];
if(scrollBeginingPoint.y > currentPoint.y && currentPoint.y < -100.0f && !scrollSearchRequest) {
scrollSearchRequest = YES;
[self.startViewController.requestSearchViewController searchWithSuccessHandler:nil errorHandler:nil];
} else if (currentPoint.y > -10.0f) {
scrollSearchRequest = NO;
}
}
#pragma mark - user methods
- (void)showDetailViewWithXMRequest:(XMRequest *)request
{
[RequestManager sharedManager].currentRequest = request;
RequestDetailViewController *requestDetailViewController = [[RequestDetailViewController alloc] initWithXMRequest:request];
requestDetailViewController.startViewController = self.startViewController;
[self.navigationController pushViewController:requestDetailViewController animated:YES];
}
- (void)setRequestLists:(NSArray *)requestLists
title:(NSString *)title
sectionTitles:(NSArray *)sectionTitles
{
self.requestLists = requestLists;
self.title = title;
self.titleLabel.text = title;
self.sectionTitles = sectionTitles;
[self.tableView reloadData];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment