Created
April 7, 2015 08:27
-
-
Save reza-ryte-club/a3d59b0a3f6c391b831e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "FirstViewController.h" | |
#import "AppDelegate.h" | |
#import "Tasks.h" | |
#import "SimpleTableCell.h" | |
@interface FirstViewController () | |
@property (retain,nonatomic) NSMutableArray *tasklist; | |
@property (retain,nonatomic) NSMutableArray *courselist; | |
@property (retain,nonatomic) NSMutableArray *teacherlist; | |
@property (retain,nonatomic) NSMutableArray *datelist; | |
@property (retain,nonatomic) UITableView *tableView; | |
@end | |
@implementation FirstViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.tasklist = [[NSMutableArray alloc] init]; | |
self.courselist = [[NSMutableArray alloc] init]; | |
self.teacherlist = [[NSMutableArray alloc] init]; | |
self.datelist = [[NSMutableArray alloc] init]; | |
self.tableView = (id)[self.view viewWithTag:1]; | |
self.tableView.estimatedRowHeight = 44.0f; | |
self.tableView.rowHeight = UITableViewAutomaticDimension; | |
//start of fetching | |
NSError *error = nil; | |
AppDelegate *theDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; | |
NSManagedObjectContext *managedObjectContext = theDelegate.managedObjectContext; | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tasks" | |
inManagedObjectContext:managedObjectContext]; | |
[fetchRequest setEntity:entity]; | |
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; | |
for (Tasks *task in fetchedObjects) { | |
[self.tasklist addObject:task.topic ]; | |
[self.courselist addObject:task.course]; | |
[self.teacherlist addObject:task.teacher]; | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateFormat:@"MM dd, yyyy HH:mm"]; | |
NSString *stringFromDate = [formatter stringFromDate:task.due_date]; | |
[self.datelist addObject:stringFromDate]; | |
} | |
//end of fetching | |
// UITableView *tableView = (id)[self.view viewWithTag:1]; | |
UIEdgeInsets contentInset = self.tableView.contentInset; | |
contentInset.top = 20; | |
[self.tableView setContentInset:contentInset]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (NSInteger)tableView:(UITableView *)tableView | |
numberOfRowsInSection:(NSInteger)section | |
{ | |
// return [self.dwarves count]; | |
return [self.tasklist count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView | |
cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *simpleTableIdentifier = @"SimpleTableCell"; | |
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; | |
if (cell == nil) { | |
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; | |
cell = [nib objectAtIndex:0]; | |
} | |
NSUInteger row = [indexPath row]; | |
NSUInteger count = [_tasklist count]; | |
/* | |
cell.topicLabel.text = [_tasklist objectAtIndex:(count-1-row)]; | |
cell.courseLabel.text = [_courselist objectAtIndex:(count-1-row)]; | |
cell.teacherLabel.text =[_teacherlist objectAtIndex:(count-1-row)]; | |
cell.dateLabel.text =[ _datelist objectAtIndex:(count-1-row)]; | |
*/ | |
cell.topicLabel.text = [_tasklist objectAtIndex:row]; | |
cell.courseLabel.text = [_courselist objectAtIndex:row]; | |
cell.teacherLabel.text =[_teacherlist objectAtIndex:row]; | |
cell.dateLabel.text =[ _datelist objectAtIndex:row]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ | |
[self.tasklist exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; | |
} | |
#pragma mark UITableViewRowAction | |
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ | |
__weak FirstViewController *weakSelf = self; | |
NSLog(@"boom1 boom1"); | |
UITableViewRowAction *actionRed = | |
[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal | |
title:@"Delete" | |
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) | |
{ | |
NSLog(@"Delete!"); | |
[weakSelf.tasklist removeObjectAtIndex:indexPath.row]; | |
[weakSelf.tableView setEditing:YES animated:YES]; | |
[weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] | |
withRowAnimation:UITableViewRowAnimationAutomatic]; | |
}]; | |
UITableViewRowAction *doneAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Done" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ | |
NSLog(@"Delete!"); | |
[self.tableView setEditing:NO]; | |
}]; | |
doneAction.backgroundColor = [UIColor greenColor]; | |
NSLog(@"boom boom"); | |
NSLog(@"%@",doneAction); | |
NSLog(@"%@",actionRed); | |
return @[actionRed]; | |
} | |
/* | |
* Must implement this method to make 'UITableViewRowAction' work. | |
* | |
*/ | |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ | |
} | |
#pragma mark - UITableViewDelegate | |
//Displaying | |
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ | |
// cell.contentView.backgroundColor = [UIColor blueColor]; | |
// | |
cell.backgroundColor = [UIColor purpleColor]; | |
} | |
//Editing Table Rows | |
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ | |
/* | |
return UITableViewCellEditingStyleNone when entering on "Reorder Mode". | |
This will make the "Move control" appear but without the "minus circle" although the tableCell will indent | |
(we still need to specify that we don't want this indentation) | |
return UITableViewCellEditingStyleDelete in order to make the "UITableViewRowAction" work. | |
I couldn't find any explanation in the docs (Xcode 6 beta 6), I've come to this conclusion through trial & error :P | |
*/ | |
return UITableViewCellEditingStyleDelete; | |
} | |
//Don't indent the cell 'cause only the move control is going to appear (see: editingStyleForRowAtIndexPath ) | |
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{ | |
return NO; | |
} | |
//selection | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ | |
NSLog(@"Selected: %@", indexPath); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment