如何根據裏面的標籤大小來佈置UITableViewCell?
正如你在照片中看到的,有兩個標籤1個foursquare圖標和1個個人資料照片。第一個標籤不會動態調整大小。其次是不正確對齊。
我能做些什麼來解決這個問題?這是什麼常見模式?
現在我在中間的另外一個問題。標題變大時,它不會下降。任何解決方案
更新:我解決它在layoutSubviews中,如你所說。非常感謝你!
如何根據裏面的標籤大小來佈置UITableViewCell?
正如你在照片中看到的,有兩個標籤1個foursquare圖標和1個個人資料照片。第一個標籤不會動態調整大小。其次是不正確對齊。
我能做些什麼來解決這個問題?這是什麼常見模式?
現在我在中間的另外一個問題。標題變大時,它不會下降。任何解決方案
更新:我解決它在layoutSubviews中,如你所說。非常感謝你!
您需要用某種方法動態計算tableview單元格的高度。
我個人使用類似的實現代碼如下格式的Twitter,只是簡單地創建單元爲您定製單元.H .M,
你可以找到原來的例子on github
特別是看看下面的(void)layoutSubviews
碼
看這個tutorial以及
yourcustomcell.h
#import <UIKit/UIKit.h>
@interface TwitterFeedCell : UITableViewCell {}
@end
您的自定義cell.m
#import "TwitterFeedCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation TwitterFeedCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[[self detailTextLabel] setLineBreakMode:UILineBreakModeWordWrap];
[[self detailTextLabel] setNumberOfLines:NSIntegerMax];
[[[self imageView] layer] setMasksToBounds:YES];
[[[self imageView] layer] setCornerRadius:5.0];
//[self setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect rect = [[self imageView] frame];
rect.origin.x = 5.0;
rect.origin.y = 5.0;
[[self imageView] setFrame:rect];
rect = [[self textLabel] frame];
rect.origin.x = 60.0;
rect.origin.y = 5.0;
[[self textLabel] setFrame:rect];
rect = [[self detailTextLabel] frame];
rect.origin.x = 60.0;
rect.origin.y = 27.0;
[[self detailTextLabel] setFrame:rect];
}
@end
打電話給你的自定義單元格級上的小區標識
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"tweets";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TwitterFeedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell = [[TwitterFeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.textLabel.text
//cell.detailTextLabel.text
//cell.imageView.image
}
使用這種方法來計算串
+ (CGSize) calculateLabelHeightWith:(CGFloat)width text:(NSString*)textString
{
CGSize maximumSize = CGSizeMake(width, 9999);
CGSize size = [textString sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:14]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
+ (CGSize) calculateLabelWidthOfString:(NSString*)textString withFont:(UIFont*)font
{
CGSize maximumSize = CGSizeMake(9999, 22);
CGSize size = [textString sizeWithFont:font
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
的高度和寬度,但你仍然需要動態設置高度表觀察室
爲
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return <height>;
}
但origin.y detailTextLabel應根據被改變textLabel的大小。如何處理? – Burak
在我的答案你不能處理它。你需要設置你的'cell.textLabel.text =用戶名+說;'然後你的'detail.textlabel.text =來自用戶的提要。請參閱上面的代碼,您可能需要稍微修改一下這些圖片。 http://s3.amazonaws.com/cocoacontrols_production/ios_screens/237/full.png?1306257816 –