2013-05-12 44 views
3

我也跟着上添加子視圖到UITableViewcellhere顯示勾選附件視圖中移動表內容

到3號的標籤添加到細胞Apple的文檔。問題是,當我顯示右側的標準複選標記附件視圖時,即使沒有顯示第三個標籤,整個單元格內容仍然向左移動。 屏幕下方有pic

這裏是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"MyCell"; 

    UILabel *mainLabel, *secondLabel, *thirdLabel; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 2.0, 100.0, 21.0)]; 
    mainLabel.tag = MAINLABEL_TAG; 
    //mainLabel.font = [UIFont systemFontOfSize:14.0]; 
    //mainLabel.font = [UIFont fontWithName:@"Geeza Pro Bold" size:17.0]; 
    mainLabel.font = [UIFont boldSystemFontOfSize:17]; 
    mainLabel.textAlignment = UITextAlignmentLeft; 
    mainLabel.textColor = [UIColor blackColor]; 
    mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
    [cell.contentView addSubview:mainLabel]; 

    secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 22.0, 100.0, 21.0)]; 
    secondLabel.tag = SECONDLABEL_TAG; 
    //secondLabel.font = [UIFont systemFontOfSize:12.0]; 
    //secondLabel.font = [UIFont fontWithName:@"Geeza Pro" size:15.0]; 
    secondLabel.font = [UIFont systemFontOfSize:15]; 
    secondLabel.textAlignment = UITextAlignmentLeft; 
    secondLabel.textColor = [UIColor darkGrayColor]; 
    secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
    [cell.contentView addSubview:secondLabel]; 

    thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(203.0, 11.0, 70.0, 21.0)]; 
    thirdLabel.tag = THIRDLABEL_TAG; 
    //thirdLabel.font = [UIFont systemFontOfSize:12.0]; 
    //thirdLabel.font = [UIFont fontWithName:@"Geeza Pro Bold" size:17.0]; 
    thirdLabel.font = [UIFont boldSystemFontOfSize:17]; 
    thirdLabel.textAlignment = UITextAlignmentRight; 
    thirdLabel.textColor = [UIColor darkGrayColor]; 
    thirdLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
    [cell.contentView addSubview:thirdLabel]; 


} 
    else 
    { 
     mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG]; 
     secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG]; 
     thirdLabel = (UILabel *)[cell.contentView viewWithTag:THIRDLABEL_TAG]; 
    } 

    Car *car = [self.dataModel carAtIndex:indexPath.row]; 
    NSString *carName = [NSString stringWithFormat:@"%@",car.deviceName]; 
    NSString *carDetails = [NSString stringWithFormat:@"%@ at %@",car.date,car.location]; 
    NSString *carSpeed = [NSString stringWithFormat:@"%@ km/h",car.speed]; 


    mainLabel.text = carName; 
    secondLabel.text = carDetails; 
    thirdLabel.text = carSpeed; 

return cell; 
} 

更新:我根據@MattG建議改變UIViewAutoresizingFlexibleLeftMarginUIViewAutoresizingFlexibleWidth。現在,當一個單元格被選中時,標籤不會移動到左側,而是像下面的圖片一樣被部分覆蓋。 enter image description here

也許這是由我的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath造成的?下面是它的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    } 
    else 
    { 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
} 
+0

看看這個教程 - 它應該解決您的問題http://www.edumobile.org/iphone/iphone-programming-tutorials/impliment-a-custom-accessory-view-for-your-uitableview-in-iphone/ – 2013-05-12 19:07:31

+0

這個博客也很有幫助http://joshgrenon.com/2009/07/20/iphone-development-how-to-create-checkmarks-in-a-uitableview/ – 2013-05-12 19:09:46

+1

看起來你應該做的UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth。不是UIViewAutoresizingFlexibleLeftMargin。 – 2013-05-12 20:30:07

回答

0

我會通過審查蘋果的Table View Programming Guide for iOS開始。它有一個名爲'A Closer Look at Table View Cells'的部分,它打破了預定義單元格的佈局。

查看你的代碼你已經有足夠的在你的單元格中考慮UITableViewCell的子類來產生你自己的自定義單元格。您可以將您的單元格標籤的所有設置代碼都刪除到一個類中,只需考慮配置cellForRowAtIndexPath:方法中的單元格即可。

但是你的問題是關注表格視圖單元格的佈局。將子視圖背景設置爲不同的顏色有時候很有用,這樣您可以看到哪個子視圖使用了哪些空間,以及如果我們引入其他子視圖(在這種情況下爲輔助視圖),它如何與其交互。

要解決您的問題,第三個標籤的文本被截斷將首先配置標籤使用較小的字體大小。我會爲所有的標籤做這個。標籤現在應該根據標籤的寬度調整文本。您還應該檢查您的文本對齊屬性以確認它們設置爲最佳選項。

thirdLabel.adjustsFontSizeToFitWidth = YES; 
thirdLabel.minimumScaleFactor = 0.4f; 

這些更改的結果可能足以解決截斷並保留您擁有的佈局。

相關問題