用字符串中的解析json填充我的tableview

时间:2014-04-16 04:31:29

标签: ios objective-c json uitableview

对iOS来说很新,对于该做什么有点失落。我实际上有我需要的一切,我只需要帮助将所述信息放入我的tableview中。当我将鼠标悬停在我的" init"中的变量时,我正在调试并且一切正确函数,但是当我进入处理单元格的函数时,某些事情就是没有点击。拥有您需要的信息非常令人沮丧但却不知道如何将其放在您想要的位置。

我讨厌这些问题但是我已经用尽所有其他选择了。在发布之前,我已经尝试了一切。我试图谷歌其他项目,我可以参考,没有找到任何iOS 7或与我的项目相关。我为这个基本问题道歉,并提前感谢你。

MasterViewController.m

@interface MasterViewController () {
    __block NSString * jsonString;
}
@end

@implementation MasterViewController
@synthesize coursesController = _coursesController;
-(void)getJSONString
{
    jsonString = [JSONHelper JSONgetString:@"http://iam.colum.edu/portfolio/api/course?json=True"];
}

-(void)initCourses
{
    [_coursesController.masterCoursesList
        removeLastObject];

    NSError *coursesError = nil;

    NSArray *coursesNameList =
    [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]

                                    options:NSJSONReadingMutableContainers

                                      error: &
     coursesError];
    if(coursesError)
        NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]),
              NSStringFromSelector(_cmd), coursesError.localizedDescription);

    NSArray *coursesNames = [coursesNameList objectAtIndex:0];
   for (NSString *courseName in coursesNameList) {
        NSString *stringToUse = [courseName substringFromIndex:8];

       //initialize variables
       NSString *name = courseName;
        NSInteger *courseid = 00;
        NSString *imageUrl = nil;

        //add current building to list
        [_coursesController masterCoursesListWithName:name AndcourseID:courseid AndimageURL:imageUrl];

    }


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CoursesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Courses *courseAtIndex = [self.coursesController objectInListAtIndex:indexPath.row];
    [[cell textLabel] setText:courseAtIndex.courseName];

    return cell;
}

CoursesDataController.m

@interface CoursesDataController()
-(void)initializeDefaultDataList;
@end

@implementation CoursesDataController
@synthesize masterCoursesList = _masterCoursesList;

-(void)initializeDefaultDataList
{
    NSMutableArray *coursesList = [[NSMutableArray alloc] init];

    Courses *course = [[Courses
                        alloc]initWithName:@"Loading Now..." AndcourseID:00];
    [coursesList addObject:course];
}

-(void)setMasterCoursesList:(NSMutableArray *)newCourseList
{
    if(_masterCoursesList != newCourseList)
    {
        _masterCoursesList = [newCourseList mutableCopy];
    }
} //this function is created when you create a NSMutableArray MasterCoursesList

-(id)init
{
    if (self = [super init])
    {
        [self initializeDefaultDataList];

        return self;
    }

    return nil;
}

-(NSUInteger) countOfList
{
    return [self.masterCoursesList count];
}

-(Courses *)objectInListAtIndex:(NSInteger)theIndex
{
    return [self.masterCoursesList objectAtIndex:theIndex];
}

-(void)masterCoursesListWithName:(NSString *)cName
      AndcourseID:(NSInteger *)cID
      AndimageURL:(NSString *)cURL
{
    Courses *courses;
    courses = [[Courses alloc] initWithName:cName
                                AndcourseID:cID
                                AndimageURL:cURL];
    [self.masterCoursesList addObject:courses];
}

-(void)courseName:(NSString *)cName
      AndcourseID:(NSInteger *)cID
{
    Courses *courses;
    courses = [[Courses alloc] initWithName:cName AndcourseID:cID];
    [self.masterCoursesList addObject:courses];
}



@end

CoursesDataController.h

#import <Foundation/Foundation.h>
@class Courses;

@interface CoursesDataController : NSObject


@property(nonatomic, copy) NSMutableArray *
masterCoursesList;
-(NSUInteger) countOfList;
-(Courses *)objectInListAtIndex:(NSInteger)theIndex;
- (void)masterCoursesListWithName:(NSString *)cName
                AndcourseID:(NSInteger *)cID
                AndimageURL:(NSString *)cURL;

- (void)courseName:(NSString *)cName
       AndcourseID:(NSInteger *)cID;


@end

JSONHelper.m

+(NSString *)JSONgetString:(NSString *)query
{

    NSString* searchURL = [NSString stringWithFormat:query];

    NSError* error = nil;               //error for NSUSRLConnection
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];

    NSURL* URL = [NSURL URLWithString:searchURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];

    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if (error)
    {
        NSLog(@"Error performing request %@", searchURL);
        return 0;
    }

    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"jsonString:%@", jsonString);
    return jsonString;
}

我发布了很多代码,但是只有几行对我的问题非常重要。当我将鼠标悬停在&#34; name&#34;在以下代码行[_coursesController masterCoursesListWithName:name AndcourseID:courseid AndimageURL:imageUrl];中 我得到了我需要的东西(它循环遍历每个JSON数组并显示每一个)。但是在处理我的单元格的函数中,当我将鼠标悬停在courseName上时,它并没有说什么。

Courses *courseAtIndex = [self.coursesController objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:courseAtIndex.courseName];

编辑:

MasterViewController.m中的numberOfRowsInSection函数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.coursesController countOfList];
}

1 个答案:

答案 0 :(得分:0)

试试这个,

CoursesDataController.m

中的

-(void)initializeDefaultDataList
{
    self.masterCoursesList = [[NSMutableArray alloc] init];

    Courses *course = [[Courses
                        alloc]initWithName:@"Loading Now..." AndcourseID:00];
    [self.masterCoursesList addObject:course];
}
相关问题