我有一个标签栏,第一个视图需要登录 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