2012-08-24 41 views
0

我很擔心這個問題。我有一個NSArray *列表,它包含一個plist文件中的70個元素。現在我需要將它們填充到UITableView。 我想要的是一次顯示20個項目,然後單擊第21個單元格以顯示另一個20 ..然後按第41個單元格以顯示另外20個項目...等等,直到所有72個單元格都顯示出來..一次顯示一定數量的行,單擊按鈕,顯示更多

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    return 20; 

} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
static NSString *CellIdentifier = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

static NSString *addCellID = @"addMoreCell"; 
UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:addCellID]; 

if (indexPath.row == 0) { 
    static NSString *CellIdentifier = @"labelCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    return cell; 
} else if (indexPath.row < 25){ 
    NSDictionary *dic = [self.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [dic objectForKey:@"Title"]; 
    cell.detailTextLabel.text = [dic objectForKey:@"Tips"]; 
    return cell; 
} else { 
    if (cell == nil) { 
     addCell.textLabel.text = @"Load More..."; 
     return cell; 
    } 
} 
} 


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(indexPath.row == 20) 
{ 
    [self.tableView reloadData]; 
} 
} 
+1

調查UITableView部分http://www.iphonesdkarticles.com/2009/01/uitableview-sectioned-table-view.html – FaddishWorm

回答

2

以一個數組,其中你會add 20 elements from plist

設置numberOfRowsInSection = [array count]

檢查,如果在plist中numberOfRowsInSection < totalNumberOfRecords,如果小於,則increment numberOfRowsInSection by 1.

而在的cellForRowAtIndexPath 檢查if (indexPath.row < totalNumberOfRecords -2 && indexPath.row < numberOfRowsInSection-1)

然後加"LoadMore Cell"否則不行。

+0

我有類似的情況。我有76個物體(約)陣列。在填充tableview時,table一次只能顯示10個單元格。首先0-10個對象,然後按下一個按鈕表應顯示11-20數據,然後21-30等...所以我怎麼能這樣做?你有什麼想法嗎? – Moxarth

1

所以我做了一些非常相似的事情。你有一個包含70個字符串(或字典)的數組來顯示在表中。 'numberOfRowsInSection:'應該使用最初設置爲20的伊娃。

當你按下按鈕時,你將伊娃增加20(但不超過實際的數據量!),你重新加載表,那麼你可以使用tableView方法將表格滾動到「新」單元格。