當前在我的tableView中向上滾動時,我發現了一個奇怪而煩人的錯誤。當我決定釋放「滾動」時,意味着我放棄了「滾動」,所以「視圖」將返回到顯示所有TableView內容的正常位置,因爲某些原因,我的某些單元格可能會在寬度。我不知道爲什麼會發生這種情況,或者是什麼問題。向上滾動後UITableViewCell的高度變化?
我的單元格(commentLabel)是根據我的論壇中標籤的高度自定義爲fitSize。我認爲問題可能在於我如何自定義單元格的內容。我會發布我的相關代碼,併發布到下面的圖片。
開始向上拖動滾動前:http://tinypic.com/view.php?pic=2rfsum9&s=6
發佈後/再DROP掉滾動到其正常位置。現在,小區的一個變化:http://tinypic.com/view.php?pic=swxnqv&s=6
代碼:
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Feedback *item = [self.items objectAtIndex:indexPath.row];
UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];
[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];
commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];
return cell;
}
-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
CGSize maximumSize = CGSizeMake(labelWidth, 10000);
//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:12.0f]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeTailTruncation];
return labelHeighSize.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feedback *item = [self.items objectAtIndex:indexPath.row];
CGFloat commentTextHeight = [self getLabelHeightForText:item.comment andWidth:162];
return commentTextHeight + 39;
}
編輯
新代碼:
CGRect frame = commentLabel.frame;
frame.size.width = kLabelWidth;
frame.size.height = 10000;
commentLabel.frame = frame;
frame = commentLabel.frame;
frame.size.height += kOffset;
commentLabel.frame = frame;
但我做錯了什麼SI現在從commentLabel的文本沒有得到正確顯示,任何人都可以在我的錯誤在這裏如何設置框架?這是細胞的樣子現在http://oi50.tinypic.com/2w5u0s6.jpg
新全碼:
static const CGFloat kLabelWidth = 162;
static const CGFloat kOffset = 39;
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Feedback *item = [self.items objectAtIndex:indexPath.row];
UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];
[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];
CGRect frame = commentLabel.frame;
frame.size.width = kLabelWidth;
frame.size.height = 10000;
commentLabel.frame = frame;
frame = commentLabel.frame;
frame.size.height += kOffset;
commentLabel.frame = frame;
commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];
return cell;
}
in'heightForRowAtIndexPath' getLabelHeightForText'返回什麼?有沒有什麼機會'item.comment'可以是零,它返回零?請記住,細胞被重新使用。 – yfrancis