2011-02-17 39 views
1

我已經在這個問題上幾個星期了。(自定義)滾動後UITableViewCell的混合

基本上當我向上滾動的TableView塔爾中/下使用在IB設計了一個自定義單元格的所有內容都混合起來,放錯地方的

香港專業教育學院嘗試多種解決方案,但都無濟於事,你要去必須原諒我的代碼一點點。

人們總是建議爲表格單元格創建一個子視圖,但我不知道該怎麼做= /對於iOS開發還是一個相當新的東西,所以如果你有一個可能的答案,你能否儘可能的詳細說明一下。

再次,對不起,我的代碼=/

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

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    NSInteger intCellTag; 

    NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row]; 


    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; 


     [[[NSBundle mainBundle] loadNibNamed:@"EventsCustomTVCell" owner:self options:nil] lastObject]; 
     cell = tvCell; 
     self.tvCell = nil; 


     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     cell.tag = intCellTag; 


     intCellTag++; 

     UIImage *customCellBG = [UIImage imageNamed:@"EventsCustomTableCellBG.png"]; 
     UIImageView *customCellBGImageView = [[UIImageView alloc] initWithImage: customCellBG]; 
     customCellBGImageView.contentMode = UIViewContentModeScaleToFill; 
     cell.backgroundView = customCellBGImageView; 
     [customCellBGImageView release]; 




     [cell.contentView addSubview:imgThumbnail]; 
     [cell.contentView addSubview:lblName]; 
     [cell.contentView addSubview:lblDescription]; 
     [cell.contentView addSubview:lblDate]; 

    } 



    imgThumbnail.image = [UIImage imageNamed:[dictionary objectForKey: @"Thumbnail"]]; 
    lblName.text = [dictionary objectForKey:@"Title"]; 
    lblDescription.text = [dictionary objectForKey:@"Description"]; 
    lblDate.text = [dictionary objectForKey:@"Date"]; 


    return cell; 


} 

回答

1

它看起來像你試圖混合隱喻來定義每個UITableViewCell - 從.xib加載,並手動創建子視圖。沒有錯,當然,但你可以直接把圖像和標籤進入tableviewCell,就像這樣:

enter image description here

和這裏的顯示每行代碼(自然在IB您分配非零個獨特的標籤給每個對象的UIKit要自定義在每行的基礎上)

#define kImageTag 1 
#define kLabel1Tag 2 
#define kLabel2Tag 3 
#define kLabel3Tag 4 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"MyTvCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     self.tvCell = nil; 
     [[NSBundle mainBundle] loadNibNamed:@"TvCell" owner:self options:nil]; 
     cell = self.tvCell; 
    } 

    UIImageView *iv = (UIImageView *) [cell viewWithTag:kImageTag]; 
    UILabel *lbl1 = (UILabel *) [cell viewWithTag:kLabel1Tag]; 
    UILabel *lbl2 = (UILabel *) [cell viewWithTag:kLabel2Tag]; 
    UILabel *lbl3 = (UILabel *) [cell viewWithTag:kLabel3Tag]; 

    iv.image = [UIImage imageNamed:@"myimage.png"]; 
    lbl1.text = @"howdy"; 
    lbl2.text = @"there"; 
    lbl3.text = @"foo"; 

    return cell; 
} 
+0

這工作就像一個魅力! – Andyy 2011-02-18 02:54:49

0

你加入同一子視圖對象,每個單元

[cell.contentView addSubview:imgThumbnail]; 
[cell.contentView addSubview:lblName]; 
[cell.contentView addSubview:lblDescription]; 
[cell.contentView addSubview:lblDate]; 

每個細胞都需要自己的一組子視圖對象。

0
imgThumbnail 
lblName 
lblDescription 
lblDate 

您應該爲這些對象分配一個標記(您可以在界面構建器中執行此操作)。
而且,如果您配置單元格,則可以查詢標記的單元格並設置其屬性。

現在您保留對最後添加的單元格的標籤的引用,並且每次表格要求您更改這些標籤的新單元格時。

比方說,你在InterfaceBuilder中指定標籤10到標籤lblDescription

那麼您需要更換

lblDescription.text = [dictionary objectForKey:@"Description"]; 

UILabel *lblDescription = (UILabel *)[cell viewWithTag:10]; 
lblDescription.text = [dictionary objectForKey:@"Description"]; 

編輯:我認爲imgThumbnail等都是子視圖您單元格,但是你再次添加它們。如果我的假設是正確的,你應該擺脫[cell.contentView addSubview ...]。

如果我錯了,你應該擺脫imgThumbnail等作爲您的viewcontroller的實例變量。每次創建新單元格時添加單獨的UIViews。就像你用backgroundview做的一樣。但是當您配置單元格值時,您已分配標籤並使用該標籤。

+0

OMG您的傳奇!有效!我對標籤和對象ID感到困惑。 DOH!但我不能讓圖像留下來,我如何在圖像上放置標籤? – Andyy 2011-02-17 14:57:25

+0

仍然卡住它,任何機會,你可以張貼我的演示代碼? thx :) – Andyy 2011-02-17 15:41:41

0

您可以使用

的NSString * cellIdentifier = [的NSString stringWithFormat:@ 「%d」,indexPath。行];

代替

靜態的NSString * CellIdentifier = @ 「小區」;

1
iOS Swift: I have done following to resolve my issue 

let CellIdentifier: String = "CellIdentifier\(indexPath.section)\(indexPath.row)" 

     var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell? 

     if cell == nil { 
      cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier) 
     }