我在UITableViewCell中有三個自定義的UILabel。像這樣的單元設計,在iPhone中動態更改UITableviewCell高度?
Date(12.1.2012) Time(12.00pm)
Cost: $20.00
Details
我已經在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
中指定了行高100。但是,現在細節有時會從服務器返回空(空)。那時我需要從單元格中移除細節標籤並將特定的單元格(行)高度更改爲70.我該怎麼做?我在Google中最好地搜索了我的級別。但是,我仍然只是困惑這樣做。你能幫我麼?謝謝。我從這個鏈接中得到了一些想法 - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
。他們只使用一個標籤來調整行高。請幫幫我。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
dateLabel.tag = 100;
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
dateLabel.font = [UIFont boldSystemFontOfSize:16];
dateLabel.highlightedTextColor = [UIColor whiteColor];
[cell.contentView addSubview: dateLabel];
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 200, 25)];
timeLabel.tag = 101;
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
timeLabel.highlightedTextColor = [UIColor whiteColor];
[cell.contentView addSubview: timeLabel];
UILabel *costLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 25)];
costLabel.tag = 102;
costLabel.backgroundColor = [UIColor clearColor];
costLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
costLabel.font = [UIFont boldSystemFontOfSize:14];
costLabel.highlightedTextColor = [UIColor whiteColor];
[cell.contentView addSubview: costLabel];
UILabel *eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 25)];
eventLabel.tag = 103;
eventLabel.backgroundColor = [UIColor clearColor];
eventLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
eventLabel.font = [UIFont boldSystemFontOfSize:14];
eventLabel.highlightedTextColor = [UIColor whiteColor];
[cell.contentView addSubview: eventLabel];
}
UILabel *dateLabel = (UILabel *) [cell.contentView viewWithTag:100];
dateLabel.text = [DateArray objectAtIndex:indexPath.row];
UILabel * timeLabel = (UILabel *) [cell.contentView viewWithTag:101];
timeLabel.text = [timeArray objectAtIndex:indexPath.row];
UILabel * costLabel = (UILabel *) [cell.contentView viewWithTag:102];
costLabel.text = [costArray objectAtIndex:indexPath.row];
UILabel *eventLabel = (UILabel *) [cell.contentView viewWithTag:103];
NSString *description = [eventArray objectAtIndex:indexPath.row];
if([description isEqualToString:@""])
{
eventLabel.text = @""; // Here i don't want to show this label and resize(reduce) the row height to 60;
}
else
{
eventLabel.text = description;
}
return cell;
}
我該怎麼做?謝謝。
Mr.MadhavanRP感謝您的回答。我已經在我的代碼中嘗試了您的答案。我檢查了cellForRowAtIndexPath中的條件:也。第一次它工作正常。當我滾動tableview時,單元格大小已更改,說明標籤已更改位置。這意味着說明標籤已刪除所有行。你能幫我麼。謝謝 – Gopinath 2012-01-13 07:17:37
@Gopinath爲什麼不嘗試添加細節標籤作爲單元格的子視圖,只有當你有一些描述。您可能需要創建一個自定義類來解決滾動問題。參考這個問題的答案http://stackoverflow.com/questions/8352530/changing-the-position-of-custom-uibutton-in-custom-uitableviewcell – MadhavanRP 2012-01-13 08:48:50