2012-04-13 56 views
0

在我的应用程序中,我显示项目的。我的JSON供稿包含50个项目,但首先我想显示25,当用户向下滚动到最后一行时,需要加载更多项目。检测最后一行并添加一个

我做:

下载JSON饲料;

NSString *jsonString = [NSString 
         stringWithContentsOfURL:[NSURL URLWithString:xmlDataUrl] 
         encoding:NSStringEncodingConversionAllowLossy 
         error:nil]; 
SBJSON *parser = [[SBJSON alloc] init]; 
NSDictionary *results = [parser objectWithString:jsonString error:nil]; 
parser = nil; 
[self setTableData:[results objectForKey:@"feed"]]; 

//

- (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. 
     //return [tableData count]; 
     return 25; 
    } 

,并检测最后一行:

if(indexPath.row == [self.tableData count] - 1) 
    { 

     NSLog(@"Last Row"); 
    } 

但它没有检测到最后一排。这是因为它下载的JSON包含超过25行?

而我怎么可以添加一个新的行就像[self.tableDate count] +1;可能吗?

如果我回来[tableData count];它的检测的最后一排,但行已经填补了100%我需要显示25/50比+1

+0

您将无法添加行,因为您将25代码硬编码为该部分中的行数。 – 2012-04-13 20:57:54

+0

我可以做; 'return [tableData count];'然后它会下载显示所有的项目,我可以设置一个最大和以后的+1? – Jones 2012-04-13 21:01:30

回答

2

你在哪里调用的代码,第二块?它在你的表代表的willDisplayCell:方法吗?

在我的项目之一,我有非常相似的代码

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == self.search.count && [tableView.indexPathsForVisibleRows containsObject:indexPath]) 
    { 
     [self.search findMoreStuff]; 
    } 
} 

有了一个额外的一行self.search中创建的附加后,所以在我的情况下,我检查self.search.count ,但您可能想要查看self.search.count-1

+0

你的第二块代码是什么意思? '最后一排'部分?在forRowAtIndexPath中。 – Jones 2012-04-13 21:06:45

+0

对不起,我的意思是if(indexPath.row == [self.tableData count] - 1) 一行。必须在willDisplayCell中调用: – 2012-04-13 21:43:22

相关问题