当我在 tableView 的最后一行单击“更多数据...”时,TableView 不会重新加载数据

Posted

技术标签:

【中文标题】当我在 tableView 的最后一行单击“更多数据...”时,TableView 不会重新加载数据【英文标题】:TableView does not reloaddata when I clicked "More data..." at the last row of tableView 【发布时间】:2012-03-19 15:59:51 【问题描述】:

向我的测试webService请求数据,

tableView 的最后一行是“More data...”

当点击该行时,发送另一个请求以获取更多数据,

我用了很多次[tableView reloaddata],但是什么都没有

发生了,我不知道为什么。所以,请帮我解决这个问题。

提前谢谢你。

还有我的 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

【问题讨论】:

——Jxdev 抱歉,这是很多代码。您能否告诉我们 numberOfRowsInSection 在您调用“更多数据...”后是否返回正确的行数?看起来有一种情况,它返回未初始化的theNumberOfRowsInSection NSLog(@"theNumberOfRowsInsection:%d",theNumberOfRowsInSection) 在我调用“更多数据”后,数字没有改变 您在执行“更多数据...”后是否更新您的allNoticeArray?我认为您的模型不是最新的,无法反映您的 tableView 中的更改。 .... 很抱歉,我只是忘记将文件的所有者连接到 .xib 文件中的 IBOutlet tableView!这就是reloaddata不起作用的原因!我真是个白痴。OLO!谢谢 !尼克韦弗 【参考方案1】:

改变:

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

到:

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

原因是数组(和行)索引是以 0 为基数的。所以如果一个数组有 3 个对象,最后一个对象的索引是 2

【讨论】:

"theNumberOfRowsInSection = [allNoticeArray count]+1";如果有更多数据,我在theNumberOfRowsInSection函数中+1行。只有没有数据可以获取,“theNumberOfRowsInSection = [allNoticeArray count]”然后我不需要获取更多数据 我犯了一个如此愚蠢的错误!另外,谢谢!哈哈,祝你有美好的一天,Alladinian。【参考方案2】:

顺便说一句,有了新的语言特性,就不需要在界面中声明ivars了。如果您已经声明了属性并合成了它们,编译器会处理它们。

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

    NSArray *addNoticeArray;

    NSInteger totalNotice;

    NSInteger pageIndex;

    UITableView *tableView;

//...
@end

【讨论】:

以上是关于当我在 tableView 的最后一行单击“更多数据...”时,TableView 不会重新加载数据的主要内容,如果未能解决你的问题,请参考以下文章

在 tableview 中插入新行会重复最后一行

Swift UISearchController tableview 搜索结果滚动不会在键盘上方移动到最后一行

在ios7中删除TableView的最后一行时出现动画问题

当我在 swift 4 中单击 uitableviewcell 时如何传递数据?

防止在第一次单击后检查最后一个单元格行,反之亦然&将选定的行添加/删除到数组

addSubview 到特定的 uitableviewcell 上选择