2009-09-30 102 views
1

我正在第一次在目標c中工作,並遇到了一個問題,我沒有看到答案。可可觸摸UITableView數據

我從JSON數據集中加載一組數據並使用它來填充UITableViewController中的UITableView。

首先加載視圖(viewDidLoad)時,我使用來自URL的JSON數據填充數組。

接下來的數據加載,因爲它應該。 numberOfRowsInSection顯示數組中有30個結果是正確的。

但是Iphone將整個集合加載到tableview中三次。

這裏是從控制器該視圖一些代碼:(Twitter正在被用作示例,並且是未設置我使用的實際數據)

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
//results = [[NSMutableArray alloc] init]; 

[email protected]"public_timeline"; 
self.tableView.allowsSelection = NO; 
NSString *url = [[NSString alloc] init]; 
url = [[NSString alloc] initWithFormat:@"http://twitter.com/statuses/%s.json",@"public_timeline"]; 
if ([results count] == 0) { 
    [self parseJSON:url]; 
} 
    } 

Here is the parseJSON (actual parse is done with the Cocoa JSON framework 

    -(void) parseJSON:(NSString *)URL{ 
    NSURL *JSONURL = [NSURL URLWithString:URL]; 
    NSData *responseData = [[NSData alloc] initWithContentsOfURL:JSONURL]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    results = [[NSMutableArray alloc] initWithArray:[responseString JSONValue]]; 
    } 


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [results 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]; 
    // Set up the cell... 
    } 
    NSDictionary *cdict = [results objectAtIndex:indexPath.row]; 
cell.textLabel.text = [cdict valueForKey:@"text"]; 
    return cell; 
} 

我不知道我正在做的是做到這一點的最佳方式,所以如果有人能幫助我,這將是偉大的。

+0

「負荷三次」你的意思是你要每個表觀察室3份在你的表視圖?或者,你是否目睹了你的桌面加載之前你的斷點被擊中了3次? – Meltemi 2009-10-01 04:42:37

+0

3數據副本顯示在視圖單元格中。 – lrudor 2009-10-01 22:04:28

回答

0

檢查numberOfSectionsInTableView方法和改變返回值1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
}