2012-03-19 78 views
0

请求我的测试web服务的数据的最后一排,的TableView不reloaddata当我点击“更多数据...”在的tableView

的的tableView的最后一行是“更多数据...”

当单击该列,发送另一个请求,以获得更多的数据,

我用[的tableView reloaddata]很多次,但并没有什么

发生了,我不知道why.So,请帮助我这个问题。

预先感谢您。

,并有我的tableViewController.h类:

#import <UIKit/UIKit.h> 

#import "NoticeDetailViewController.h" 

@interface NoticeViewController : UIViewController 
<UITableViewDataSource,UITableViewDelegate> 
{  
    NSMutableArray *allNoticeArray; 

    NSArray *addNoticeArray; 

    NSInteger totalNotice; 

    NSInteger pageIndex; 

    UITableView *tableView; 
} 

@property (nonatomic, retain) NSMutableArray *allNoticeArray; 
@property (nonatomic, retain) NSArray *addNoticeArray; 
@property NSInteger totalNotice; 
@property NSInteger pageIndex; 
@property (nonatomic, retain) UITableView *tableView; 
- (NSMutableArray *)getNoticeList :(NSInteger)pageIndex; 
@end 

而且tableViewController.m类:

#import "NoticeViewController.h" 
#import "Notice.h" 
#import "OAURLEncodingAdditions.h" 
@interface NoticeViewController() 
@end 

@implementation NoticeViewController 
@synthesize allNoticeArray; 
@synthesize addNoticeArray; 
@synthesize pageIndex; 
@synthesize tableView; 
@synthesize totalNotice; 

- (NSMutableArray *)getNoticeList :(NSInteger)pageIndex 
    { 
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
     NSString *userId = appDelegate.user.userId; 
     NSString *departmentId = appDelegate.user.departmentId; 

     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://xxx.xxx.xx.xx/FMS/Pages/Service/FMService.svc/GetAnnouncement?userId=%@&departmentId=%@&pageIndex=%d&pageSize=%d",userId,departmentId,self.pageIndex,1]]; 
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
     [request setValidatesSecureCertificate:NO]; 
     [request startSynchronous]; 
     NSError *error = [request error]; 
     if (!error) 
     { 
      NSString *responseString = [request responseString]; 
      NSDictionary *responseDict = [responseString JSONValue]; 
      NSArray *noticeArray = [responseDict objectForKey:@"d"]; 

      NSMutableArray *arrayOfAllNotice = [[NSMutableArray alloc] init]; 

      for (NSDictionary *noticeDic in noticeArray) 
      { 
       NSString *body = [noticeDic objectForKey:@"Body"]; 
       NSString *departmentName = [noticeDic objectForKey:@"DepartmentName"]; 
       NSString *noticeId = [noticeDic objectForKey:@"Id"]; 
       NSString *isTop = [noticeDic objectForKey:@"IsTop"]; 
       NSString *readState = [noticeDic objectForKey:@"ReadState"]; 
       NSString *realName = [noticeDic objectForKey:@"RealName"]; 
       NSString *title = [noticeDic objectForKey:@"Title"]; 

       int noid = [noticeId intValue]; 
       int isto = [isTop intValue]; 
       int read = [readState intValue]; 

       Notice *notice = [[Notice alloc] initWithBody:body 
             departmentName:departmentName 
              noticeId: noid 
               isTop:isto 
              readState:read 
              realName:realName 
               title:title]; 
       [arrayOfAllNotice addObject:notice]; 
      } 
     self.addNoticeArray = [[NSArray alloc] initWithArray:arrayOfAllNotice]; 
    } 
    else 
    { 
     .... 
    } 
    [allNoticeArray addObjectsFromArray:addNoticeArray];  
    NSLog(@"allNoticeArray count: %d",[allNoticeArray count]); //Here:When the last row clicked, the number changes:1->2 
    [self.tableView reloadData]; 
    return allNoticeArray; 
} 

#pragma mark - 
#pragma mark Table View Data Source Methods 

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger theNumberOfRowsInSection; 

    if ([allNoticeArray count] < (self.totalNotice)) 
    { 
     theNumberOfRowsInSection = [allNoticeArray count]+1; 
    } 
    if ([allNoticeArray count] == (self.totalNotice)) 
    { 
     theNumberOfRowsInSection = [allNoticeArray count]; 
    } 

    return theNumberOfRowsInSection; 
} 

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView reloadData]; 

    static NSString *NoticeListTableIdentifier = @"NoticeListTableIdentifier"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NoticeListTableIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NoticeListTableIdentifier] autorelease]; 
    } 

    if ([allNoticeArray count] < (self.totalNotice)) 
    { 
     if ([indexPath row] != [allNoticeArray count]) 
     { 
      NSUInteger row = [indexPath row]; 
      Notice *noticeOfTheRow = [allNoticeArray objectAtIndex:row]; 
      NSString *title = noticeOfTheRow.title; 
      cell.textLabel.text = title; 
     } 
     else 
     { 
      cell.textLabel.text = @"More..."; 
     } 
    } 
    if ([allNoticeArray count] == (self.totalNotice)) 
    { 
     NSUInteger row = [indexPath row]; 
     Notice *noticeOfTheRow = [allNoticeArray objectAtIndex:row]; 
     NSString *title = noticeOfTheRow.title; 
     cell.textLabel.text = title; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([indexPath row] != [allNoticeArray count]) 
    {   
     NSUInteger row = [indexPath row];  
     Notice *notice = [allNoticeArray objectAtIndex:row];   
     NSString *noticeDetailTitle = notice.title;   
     NoticeDetailViewController *noticeDetailViewController = [[[NoticeDetailViewController alloc] init] autorelease]; 
     noticeDetailViewController.title = noticeDetailTitle;   
     ticeDetailViewController.noticeIdForGet = notice.noticeId;   
     [self.navigationController pushViewController:noticeDetailViewController animated:YES];   
    } 
    if ([indexPath row] == [allNoticeArray count]) 
    {   
     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
     hud.labelText = @"reload...";   
     self.pageIndex ++;   
     [self getNoticeList:self.pageIndex];   
     [self.tableView reloadData];   
     [MBProgressHUD hideHUDForView:self.view animated:YES];   
    } 
} 

- (void)pushBack 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.allNoticeArray = [[NSMutableArray alloc] init]; 
    self.pageIndex = 1;  
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    hud.labelText = @"reload...";  
    self.title = @"Notice";  
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"back" 
                     style:UIBarButtonItemStyleBordered 
                     target:self 
                     action:@selector(pushBack)]; 
self.navigationItem.leftBarButtonItem = leftButton; 


    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"reload" 
                     style:UIBarButtonItemStyleBordered 
                    target:self 
                    action:@selector(reloadNotice)]; 
    self.navigationItem.rightBarButtonItem = rightButton; 
    self.allNoticeArray = [self getNoticeList:self.pageIndex];  
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
} 
@end 
+0

-Jxdev对不起,这是很多代码。你可以告诉我们,在你调用“更多数据...”之后,numberOfRowsInSection是否返回正确的行数?它看起来像是返回未初始化的'theNumberOfRowsInSection'的情况。 – 2012-03-19 16:05:56

+0

我调用“more data”后NSLog(@“theNumberOfRowsInsection:%d”,theNumberOfRowsInSection) – jxdwinter 2012-03-19 16:17:02

+0

在更多数据...之后更新'allNoticeArray'吗?我认为你的模型并不是最新的以反映你的tableView的变化。 – 2012-03-19 16:24:14

回答

0

变化:

if ([indexPath row] == [allNoticeArray count]) 

到:

if ([indexPath row] == [allNoticeArray count]-1) 

的原因是阵列(且行)索引是基本为0。所以,如果一个数组,说3个对象,最后目标的指数是2

+0

“theNumberOfRowsInSection = [allNoticeArray count] +1”;如果有更多的数据,我在+1行中的NumberOfRowsInSection函数。只有没有数据要获得,“theNumberOfRowsInSection = [allNoticeArray count]”然后我不需要获取更多的数据 – jxdwinter 2012-03-19 16:21:02

+0

我犯了一个如此愚蠢的错误!另外,谢谢!LOL,祝你有个美好的一天,Alladinian。 – jxdwinter 2012-03-19 16:32:58

0

BTW,用新的语言特性,没有必要申报ivars在界面中。如果你已经声明了属性并合成它们,编译器会照顾它们。

@interface NoticeViewController : UIViewController 
<UITableViewDataSource,UITableViewDelegate> 
{  
    NSMutableArray *allNoticeArray; 

    NSArray *addNoticeArray; 

    NSInteger totalNotice; 

    NSInteger pageIndex; 

    UITableView *tableView; 
} 
//... 
@end