Adding json data into UITableView

前端 未结 2 1286
-上瘾入骨i
-上瘾入骨i 2020-12-22 11:15

This is a sample getData method in my viewController.m file

-(void) getData {
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser allo         


        
相关标签:
2条回答
  • 2020-12-22 11:28

    In getData function gather all the data in an array. say it statusArray. Now, derived a viewcontroller from UItableViewController. Make a member in this class of NSArray type and assign above array to it. Write below functions into the controller class.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return statusArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        cell.textLabel.text = [statusArray objectAtIndex:indexPath.row] objectForKey:@"user"];
        return cell;
    }
    
    0 讨论(0)
  • 2020-12-22 11:45

    Implement the UITableViewDataSource protocol:

    • tableView:numberOfRowsInSection: - return the number of "statuses"
    • tableView:cellForRowAtIndexPath: - return a UITableViewCell whose textLabel.text is set to your text.

    There are plenty of examples in Apple's docs, including the project you get when you create a new app using Xcode's "Master-Detail App" template.

    0 讨论(0)
提交回复
热议问题