2011-02-10 27 views
0

我有一個tableview和自定義單元格的問題。設置TextView文本時heightForRowAtIndexPath滾動問題

我有以下代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

// ================================================================================================= 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

// ================================================================================================= 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return 1; 
} 

// ================================================================================================= 
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"MyCustomCell"; 

    MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     UIViewController *c = [[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil]; 
     cell = (MyCustomCell*)c.view; 
     [c release]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    MyCustomCell* theCell = (MyCustomCell*)cell; 

theCell.theTextView.text = @"Label My Label"; 
} 

// ================================================================================================= 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; // -- In fact it's dynamic 
} 

使用的代碼,我在模擬器上(iOS 4.2的)一個奇怪的行爲。 我只有一個單元格(分組tableview)。當我滾動列表以使單元格消失在屏幕頂部時,然後當我釋放它時,而不是僅僅順利滾動,列表突然重新出現在屏幕上,就好像我從未滾動它然後滾動一點點(大約25px,實際上是我的TextView的頂部)到頂部。

如果我刪除或評論theCell.theTextView.text = @「標籤我的標籤」;那麼就不會有問題了。如果我只向上方滾動一點點,那麼細胞不會消失,這個問題就不存在了。如果我有很多行並使第一個單元格超過頂部,問題是相同的。

TextView位於UITableCellView內部,並且textview足夠大,可以將texte顯示爲單行文本。

你能看到問題在哪裏嗎?

如果您沒有答案,請僅徵詢其他信息或建議,讓其他人看到這個問題還沒有答案。

謝謝你的幫助。

+1

也許這有助於:用[c autorelease]取代[c release]。 – Felix 2011-02-10 18:56:57

回答

0

問題是沒有設置TextView的「Scrolling enabled」屬性。設置此屬性爲「開啓」,問題不復存在...