簡體   English   中英

UITableView無法平滑滾動……(iPhone SDK)..!

[英]UITableView not scrolling smoothly…(iPhone SDK) ..!

UITableView無法平滑滾動...(iPhone SDK).. !!

我已經在單獨的單獨類中實現了UITableView DataSource和Delegate方法。(一個用於委托,一個用於數據源)在主程序中我只寫:

//assume that all objects are allocated 
ObjTableView.dataSource=ObjDataSource;
ObjTableView.delegate = ObjDelegate;
[self.view addSubView: ObjTableView];

當我運行此代碼時,會出現UITable視圖,但是當我嘗試滾動它時,它不會平滑滾動。 我還檢查了初始化單元后UITableViewCell是否不會重繪。

誰能告訴我為什么會這樣? 我怎么解決這個問題 ??

來自評論:

ListDataSource *ObjListDataSource = [[ListDataSource alloc]initWithArray:[[sender object] valueForKey:@"List"]]; 
ListDelegate *ObjListDelegate = [[ListDelegate alloc]initWithArray:[[sender object] valueForKey:@"List"]];
tblList = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];  
tblList.dataSource = ObjListDataSource; tblList.delegate = ObjListDelegate; 
[self.view addSubview:tblList]; [tblShopList release];

更多評論:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,100) reuseIdentifier:CellIdentifier] autorelease]; 
        //custom cell code 
    } 
    return cell; 
}

更多信息:

我已經使用NSNotification在解析完成時通知當前類,在收到通知后,當前類方法將調用DataSource,Delegate方法(在單獨的類文件中定義)。

因此,UItableViewCell定制(在ListDataSource中)和表視圖(在當前類中)都在不同的類中。

問題是

NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 

同一類的所有單元格的ID都必須相同,否則您將永遠不會重復使用它們。 正如您在大多數示例中看到的那樣,在大多數(所有)情況下,它確實是一個常數。

關於reuseIdentifier的解釋很少:每當一個單元離開屏幕時,您就可以重用它而不是創建一個新單元。 要重用它,您需要隊列中的一個單元格具有與傳遞給dequeueReusableCellWithIdentifier相同的標識符。 按照這種方式,單元永遠不會被重復使用,因為每個ID都是唯一的(如果行重新出現在屏幕上,它們可能會或可能不會被重復使用,具體取決於隊列大小,這不是可配置的AFAIK)。 這就是為什么單元格的個性化應該在“ cell == nil ”塊之外進行的原因。 長話短說,您使用的不是預期的復用信息。

我認為Michele是正確的,但我還要補充一點,看來您正在對創建單元的單元進行自定義。 您應該做的更像是這樣:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = (UITableViewCell)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,100) reuseIdentifier:CellIdentifier] autorelease]; 
        //custom REUSABLE cell code here, e.g. text color, etc. 
    }

    NSString *cellText = [dataArray objectAtIndex:indexPath.row]; //assuming you have a simple array for your data

    cell.textLabel.text = cellText;

    return cell; 
}

我還要補充一點,因為UITableViewCell cell = ...是無效的初始化程序,所以我不確定為什么可以使用此處的代碼運行應用程序。 它應該是UITableViewCell *cell = ...

查看您如何自定義單元格將很有幫助,因為沒有它,很難看到正在發生的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM