-1

我有一个标签栏,第一个视图需要登录 alertview 并且工作正常。当我切换到下一个选项卡时,我有一个表格视图。

我正在使用 JSON 在我从表视图控制器中的 viewDidLoad 方法以及另一个登录过程调用的方法中从在线获取数据。我想要两个部分,每个部分中的数据取决于我从解析数据中获得的属性之一。

问题是,在解析数据时,表格视图方法已经完成,所以我得到 0 节和 0 行并且没有数据显示。所以我的问题是,有没有办法只重新加载 no of section 方法、 no of rows 方法和 cellForRowAtIndexPath 方法。如果我重新加载整个表格视图,我会收到无限的警报视图,要求登录:/

编辑:

     #import "BooksViewController.h"
     #import "Me.h"
     #import "SBJson.h"

     @interface BooksViewController ()

     @end

     @implementation BooksViewController

     @synthesize name = _name;
     @synthesize ID = _id;
     @synthesize extended = _extended;
     @synthesize returned = _returned;
     @synthesize Date = _date;

     -(void) dealloc
     {
         self.name = nil;
         self.ID = nil;
         self.extended = nil;
         self.returned = nil;
         [super dealloc];
     }

     - (id)initWithStyle:(UITableViewStyle)style
     {
         self = [super initWithStyle:style];
         if (self) {

             BooksBorrowed = [[NSMutableArray alloc]init];
             BorrowedDates = [[NSMutableArray alloc]init];
             BorrowedIDs = [[NSMutableArray alloc]init];
             BorrowedExtensions = [[NSMutableArray alloc]init];
             BorrowedReturns = [[NSMutableArray alloc]init];

             /*message = [[UIAlertView alloc] initWithTitle:@"Please Login with your User                            ID and Password"
              message:nil
              delegate:self
              cancelButtonTitle:@"Cancel"
              otherButtonTitles:@"Continue", nil];

              [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];*/
             //book = [[BooksViewController alloc]init];
             me = [[Me alloc]init];
         }
         return self;
     }

     - (void)MyBooks
     {
NSString *authFormatString =
@"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%d";

NSString *urlString = [NSString stringWithFormat:authFormatString, 1];

NSURL *url = [NSURL URLWithString:urlString];

NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

response1 = [contents JSONValue];
if(contents)
{
    NSLog(@"contents : %@",contents);
    BookName = [[NSString alloc]init];
    DateBorrowed = [[NSString alloc]init];
    BookID = [[NSString alloc]init];
    BookExtended = [[NSString alloc]init];
    BookReturned = [[NSString alloc]init];

    BookName = [response1 valueForKey:@"BookName"];
    BookID = [response1 valueForKey:@"BookID"];
    DateBorrowed = [response1 valueForKey:@"DateBorrowed"];
    BookExtended = [response1 valueForKey:@"Extended"];
    BookReturned = [response1 valueForKey:@"Returned"];

    [BooksBorrowed addObject:BookName];
    [BorrowedDates addObject:DateBorrowed];
    [BorrowedIDs addObject:BookID];
    [BorrowedReturns addObject:BookReturned];
    [BorrowedExtensions addObject:BookExtended];

    NSLog(@"Arrays are : %@, %@, %@, %@, %@", BooksBorrowed, BorrowedDates, BorrowedIDs, BorrowedReturns, BorrowedExtensions);
         }
     }


     - (void)viewDidLoad
     {
         [super viewDidLoad];

         // Uncomment the following line to preserve selection between presentations.
         // self.clearsSelectionOnViewWillAppear = NO;

         // Uncomment the following line to display an Edit button in the navigation bar          for this view controller.
         // self.navigationItem.rightBarButtonItem = self.editButtonItem;
         [self MyBooks];
     }

     - (void)didReceiveMemoryWarning
     {
[sup         er didReceiveMemoryWarning];
         // Dispose of any resources that can be recreated.
     }

     #pragma mark - Table view data source

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
         // Return the number of sections.
         return 1;
     }

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(         (NSInteger)section
     {
         // Return the number of rows in the section.
         NSLog(@"rows %d", [BooksBorrowed count]);
         return [BooksBorrowed 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];
         }

                  bookName = BooksBorrowed[0];

                  cell.textLabel.text = bookName;
                  return cell;
     }

     @end
4

2 回答 2

1

获取数据应该在后台线程上完成,这样 UI 就不会被阻塞。获取数据后,更新表视图数据源并重新加载表视图。这必须在主线程上完成。

下面的代码应该演示这个想法:

- (void)updateMyBooks
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Fetch data on a background thread:
        NSURL *url = ...;
        NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        if (contents) {
            NSMutableArray *newBooksBorrowed = [NSMutableArray array];

            // ... Parse JSON response and add objects to newBooksBorrowed ...

            dispatch_sync(dispatch_get_main_queue(), ^{
                // Update data source array and reload table view.
                BooksBorrowed = newBooksBorrowed;
                [self.tableView reloadData];
            });
        }
    });
}
于 2013-03-20T11:01:58.370 回答
0

你有两种方法可以使用。

等到在您当前的线程中解析结束。

-(void)aFunc {
    //your job A
    while (A is not finished) {
        // If A job is finished, a flag should be set. and the flag can be a exit condition of this while loop
        // This executes another run loop.
       [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

[yourTable reloadData];
}

或仅加载特定部分。

– reloadRowsAtIndexPaths:withRowAnimation:
– reloadSections:withRowAnimation:
– reloadSectionIndexTitles

编辑:

另一种方法是通过GCD,通过使用串行队列。dispatch_sync(),该块被入队并且该函数在该块被执行之前不会返回。

于 2013-03-20T08:21:41.653 回答