'wpf' 카테고리의 다른 글
| Xaml에 Binding을 하지 않고 컨버터를 쓰고 싶을때 (0) | 2018.03.07 |
|---|---|
| Style안에서 ConverterParameter를 Binding 하기 (0) | 2018.03.07 |
| WPF에서 MiniDump 뜨는법 (0) | 2018.01.02 |
| 다중 모니터에서 전체 화면 실행 (0) | 2017.12.07 |
| c++ DLL에서 예외 오류 잡는법 (0) | 2017.11.24 |
| Xaml에 Binding을 하지 않고 컨버터를 쓰고 싶을때 (0) | 2018.03.07 |
|---|---|
| Style안에서 ConverterParameter를 Binding 하기 (0) | 2018.03.07 |
| WPF에서 MiniDump 뜨는법 (0) | 2018.01.02 |
| 다중 모니터에서 전체 화면 실행 (0) | 2017.12.07 |
| c++ DLL에서 예외 오류 잡는법 (0) | 2017.11.24 |
TableView를 이용해서 SNS를 개발 하기위해 선두지점에 있는 Facebook 앱을 보았다.
이렇게 Label이 길수록 TableView cell row가 길게 나왔다.
이렇게 할려면 글을 길이를 계산해서 row의 높이를 계산해야 한다.
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
// [[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
이렇게 하면
이렇게 다이나믹하게 row의 높이가 변한다.
참고 : http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
하지만 나는 코드상에서 label을 만들어서 하지 않고 ib에서 만들어서 적용할려고 했다. 그래야 쉽지 ㅋㅋ
내가 응용한 코드는 다음과 같다.
//table height를 label 크기에 따라 변환
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
HugeBoardData* hd = [boardData objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [hd.title sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
size에 보면 hd.title이 있다. title의 크기를 기준으로 높이를 계산해서 반환한다.
그리고 크기를 내크기에 맞게 변환했다.
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 140.0f
#define CELL_CONTENT_MARGIN 10.0f
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HugeBoardCell *cell = (HugeBoardCell *)[tableView dequeueReusableCellWithIdentifier:HugeBoardCellIdentifier];
if (cell == nil) {
cell = [HugeBoardCell cellWithNib];
[cell.titleLabel setMinimumFontSize:FONT_SIZE];
[cell.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[cell.titleLabel setNumberOfLines:0];
[cell.titleLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[cell.titleLabel setTag:1];
}
HugeBoardData* hd = [boardData objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [hd.title sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
cell.titleLabel.text = [NSString stringWithFormat:@"%@", hd.title];
[cell.titleLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN+40, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
cell.idxLabel.text= [NSString stringWithFormat:@"%@ : ", hd.idx];
return cell;
}
| [iphone] 처음에 Logo Image 나오게하기 (Splash Image) (0) | 2011.08.24 |
|---|---|
| [iphone] TableView 아래로 스크롤시 내용이 하단에 계속 추가 되는 기능 만들기 (0) | 2011.08.19 |
| [iphone] 맥용 SVN Client SCPlugin (0) | 2011.08.09 |
| [iphone] modalviewcontroller의 parentviewcontroller 에서 reload하기 (1) | 2011.07.28 |
| [iphone] IB 를 이용해서 Custom TableViewCell 만들기 (0) | 2011.07.27 |