我正在尝试制作甘特图。 我在https://github.com/evolvIQ/iqwidgets
上找到了这个库我下载它,我试着理解如何使用它。
我有一个从服务器带来数据(JSON)的类。我将这些结果指定为[NSDictionary]
。
我尝试将IQGanttView.h
文件添加为当前视图的子视图。
IQGanttView.h
有一个名为addRow:(id<IQCalendarDataSource>)row
addRow
方法的参数为IQCalendarDataSource
,这就是我将类创建为IQCalendarDataSource
(Task对象)的原因。
我将IQCalendarDataSource
类更改为具有我的任务类的相同属性。
创建这些任务对象后,我为所有数组(任务对象)设置了一个for循环,通过addRow:(id<IQCalendarDataSource>)row
方法添加它们以显示在图表上。
我运行了代码然后跟着它,但它没有向我展示图表上的任务。
我不知道我是否遗漏了什么? 有人可以查看图书馆并告诉我,我是否遗漏了什么?
=================
这是我正在尝试的代码。
#import "GanttChartViewController.h"
#import "ASRESTAPI.h"
#import "ASUserSingleton.h"
#import "TasksObj.h"
#import "IQGanttView.h"
// when the view load I want to show the user the loading view until the data is ready to show up on the chart by calling [self setupLoadingIndicator] method
//
- (void)viewDidLoad {
[super viewDidLoad];
tasksItems = [[NSArray alloc] init];
self.ganttView = [[IQGanttView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.ganttView];
[self setupLoadingIndicator];
// Do any additional setup after loading the
[self gettheTasksItems];
}
-(void)setupLoadingIndicator
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES;
loadingView.layer.cornerRadius = 10.0;
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height);
[loadingView addSubview:activityView];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.adjustsFontSizeToFitWidth = YES;
loadingLabel.textAlignment = NSTextAlignmentCenter;
loadingLabel.text = @"Loading...";
[loadingView addSubview:loadingLabel];
[self.ganttView addSubview:loadingView];
[activityView startAnimating];
}
-(void)gettheTasksItems
{
NSString* username = [[ASUserSingleton sharedInstance]userName];
NSString* password = [[ASUserSingleton sharedInstance]password];
_tasks = nil;
[ASRESTAPI tasksListUsername:username andPassword:password completionBlock:^(NSDictionary *response, NSArray *taskArray) {
_tasks = response;
tasksItems = taskArray;
NSMutableArray *tasksObjs = [NSMutableArray array];
int i;
for (i =0; i < tasksItems.count;i++)
{
/*_tasks[@"project"][@"name"]*/ // this is how to call the dictionary object from the array
TasksObj* tasksObj = [[TasksObj alloc]initWithProjectName:tasksItems[i][@"project"][@"name"]
startDate:tasksItems[i][@"start_date"]
dueDate:tasksItems[i][@"due_date"]
estimatedhours:tasksItems[i][@"estimated_hours"]];
//tasksItems[i] objectForKey:@"project"];
[tasksObjs addObject:tasksObj];
}
dispatch_async(dispatch_get_main_queue(), ^{
[activityView stopAnimating];
for (loadingView in [self.ganttView subviews])
{
[loadingView removeFromSuperview];
}
//[loadingView removeFromSuperview];
for (TasksObj* tasksObj in tasksObjs) {
[self.ganttView addRow:tasksObj];
}
});
}];
}
我使用dispatch_async(dispatch_get_main_queue()
的原因是为了确保显示UI元素,我了解到如果我想要显示UI元素,那么我必须制作它通过主队列。
这是TasksObj的类
@interface TasksObj : NSObject <IQCalendarDataSource>
@property (nonatomic, strong) NSString* projectName;
@property (nonatomic, strong) NSDate* start_date;
@property (nonatomic, strong) NSDate* due_date;
@property (nonatomic, strong) NSNumber* estimated_hours;
-(id)initWithProjectName:(NSString*)projectName startDate:(NSDate*)start_date dueDate:(NSDate*)due_date estimatedhours:(NSNumber*)estimated_hours;
@end
@implementation TasksObj
-(id)initWithProjectName:(NSString*)projectName startDate:(NSDate*)start_date dueDate:(NSDate*)due_date estimatedhours:(NSNumber*)estimated_hours
{
self = [super init];
if (self) {
self.projectName = projectName;
self.start_date = start_date;
self.due_date = due_date;
self.estimated_hours = estimated_hours;
}
return self;
}
@end
顺便说一下,这是我从服务器获取的JSON数据
"task":
{
"id":1,
"project":{"id":1,"name":"test"},
"tracker":{"id":1,"name":"Bug"},
"status":{"id":1,"name":"New"},
"priority":{"id":4,"name":"Urgent"},
"author":{"id":1,"name":"the user name"},
"subject":"Example",
"description":"",
"start_date":"2016-02-17",
"due_date":"2016-02-23",
"done_ratio":0,
"estimated_hours":3.0,
"spent_hours":0.0,
"created_on":"2016-02-18T04:28:55Z",
"updated_on":"2016-02-22T19:09:22Z"
}