2013-01-03 60 views
0

我米試圖細胞添加至的tableView添加子視圖細胞視圖,示出奇怪的行爲

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *MyIdentifier = @"MyIdentifier"; 
     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [cell setSelectionStyle:UITableViewCellEditingStyleNone]; 
     // Set up the cell 

     cell.backgroundColor = [UIColor clearColor]; 
     UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     UIImageView *img; 
     UILabel *lbl; 
     UIImageView *backImage; 
     UILabel *textLabel; 
     UILabel *detailTextLabel; 

     NSInteger val = [indexPath row] * 3; 

     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     { 
      //*buyBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)]; 
      [buyBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; 

      //*img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)]; 
      [img setImage:[UIImage imageNamed:@""]]; 
      //*lbl = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,h)]; 
     } 
     else 
     { 
      backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)]; 
      [buyBtn setFrame:CGRectMake(257, 100, 57, 24)]; 
      img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 75, 57, 24)]; 

      lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)]; 
      textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)]; 
      detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)]; 

      backImage.image = [UIImage imageNamed:@"shop-bg.png"]; 
      [buyBtn setImage:[UIImage imageNamed:@"buy_button"] forState:UIControlStateNormal]; 
      [img setImage:[UIImage imageNamed:@"price_button.png"]]; 

      lbl.center = img.center; 
      lbl.textAlignment = UITextAlignmentCenter; 
      lbl.text = [self.original_List objectAtIndex:val+2]; 
      lbl.backgroundColor = [UIColor clearColor]; 

      textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:14]; 
      textLabel.backgroundColor = [UIColor clearColor]; 
      textLabel.text = [self.original_List objectAtIndex:val]; 

      detailTextLabel.text = [self.original_List objectAtIndex:val+1]; 
      detailTextLabel.backgroundColor = [UIColor clearColor]; 
      detailTextLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:10]; 
      textLabel.textColor = [UIColor blackColor]; 
      detailTextLabel.textColor = [UIColor whiteColor]; 
      detailTextLabel.numberOfLines = 2; 
     } 

     [buyBtn setTag:[indexPath row]]; 
     [buyBtn addTarget:self action:@selector(buyBtnPressed:) forControlEvents:UIControlEventTouchDown]; 
     [cell addSubview:backImage]; 
     [cell addSubview:detailTextLabel]; 
     [cell addSubview:textLabel]; 
     [cell addSubview:buyBtn]; 
     [cell addSubview:img]; 
     [cell addSubview:lbl]; 

     return cell; 
    } 

從圖中可以看出,第一細胞不包含所添加的圖像,標籤和按鈕,但是如果其他細胞捲動失去視野的人變得透明,而那些在視野內的人也沒問題。

爲什麼我的三個子視圖沒有從第一個單元格添加,我的textLabel和detailTextLabel是應該添加的。所有的

enter image description here

回答

1

首先,你不要出列的單元格,始終沒有創造重用一個新的。嘗試添加這樣的事情(不知道你是否需要自動釋放,取決於天氣您使用ARC與否):

RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
     cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
              reuseIdentifier:cellIdentifier] autorelease]; 
} 

第二,如果你不使用ARC(自動參考計數),那麼你有一堆失去了裁判的這裏。

[[Class alloc] init]添加了ref,addSubview也添加了ref。我想知道什麼xcode「分析」構建說這個。

第三,恕我直言,你加入到細胞標籤也需要他們的「幀」屬性設置,不只是行數等

+0

使用弧,我會試試看。我已經設置了框架。原因是你指出的第一個問題是因爲我認爲 –

+0

已經解決了問題但是一個礦石問題出現了。我有11個條目,但它只顯示7個,因爲它們在第一時間被查看,並且如果我向下滾動,它會複製前四個單元格並顯示它們。爲什麼? –

0

我這裏是我是如何做的。我創建的單元格類派生出你所需要的(在我的情況下是CopyableCell)。

這是* .h文件。

#import <UIKit/UIKit.h> 
#import "CopyableCell.h" 

@interface RecentsTableViewCell : CopyableCell 

@property (readonly, retain) UILabel *titleLabel; 
@property (readonly, retain) UILabel *detailsLabel; 
@property (readonly, retain) UILabel *subdetailsLabel; 
@property (readonly, retain) UILabel *subtitleLabel; 

@end 

這裏是* .m文件。我不是使用ARC因此,如果您使用此代碼,相應修改

#import "RecentsTableViewCell.h" 

@interface RecentsTableViewCell() 

// Redeclare property as readwrite 
@property (readwrite, retain) UILabel *titleLabel; 
@property (readwrite, retain) UILabel *subtitleLabel; 
@property (readwrite, retain) UILabel *detailsLabel; 
@property (readwrite, retain) UILabel *subdetailsLabel; 

@end 

@implementation RecentsTableViewCell 

@synthesize titleLabel; 
@synthesize subtitleLabel; 
@synthesize detailsLabel; 
@synthesize subdetailsLabel; 

- (void) layoutSubviews 
{ 
     int leftMargin = 30; 
     int rightMargin = 6; 

     [super layoutSubviews]; 

     CGRect cellRect = self.contentView.frame; 
     int width = cellRect.size.width - (leftMargin + rightMargin); 

     CGSize constraintSize = CGSizeMake(300, MAXFLOAT); 
     CGSize titleSize = [titleLabel.text sizeWithFont:titleLabel.font 
             constrainedToSize:constraintSize 
           lineBreakMode:UILineBreakModeTailTruncation]; 
     CGSize detailsSize = [detailsLabel.text sizeWithFont:detailsLabel.font 
              constrainedToSize:constraintSize 
            lineBreakMode:UILineBreakModeTailTruncation]; 

     if (titleSize.width > titleLabel.frame.size.width || 
      detailsSize.width > detailsLabel.frame.size.width) { 
       CGRect detailsFrame = detailsLabel.frame; 
       detailsFrame.size.width = detailsSize.width; 
       detailsFrame.origin.x = cellRect.size.width - 
         (detailsSize.width + subdetailsLabel.frame.size.width + rightMargin); 

       CGRect titleFrame = titleLabel.frame; 
       titleFrame.size.width = width - detailsSize.width - subdetailsLabel.frame.size.width - 5; 

       titleLabel.frame = titleFrame; 
       detailsLabel.frame = detailsFrame; 
     } 
} 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
     if (self) { 
       CGRect cellRect = self.contentView.frame; 
       cellRect.size.height = 61; 
       self.contentView.frame = cellRect; 

       int leftMargin = 30; 
       int rightMargin = 6; 
       int x = cellRect.origin.x + leftMargin; 
       int y = cellRect.origin.y; 
       int width = cellRect.size.width - (leftMargin + rightMargin); 
       int height = cellRect.size.height; 

       int titleWidth = width * 0.75; 
       int alldetailsWidth = width - titleWidth; 
       int detailsWidth = alldetailsWidth * 0.65; 
       int subdetailsWidth = alldetailsWidth - detailsWidth; 

       int titleHeight = height * 0.38; 
       int subtitleHeight = height - titleHeight; 

       self.titleLabel = [[[UILabel alloc] 
            initWithFrame:CGRectMake(x, y, titleWidth, titleHeight)] autorelease]; 
       self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0f]; 
       self.titleLabel.highlightedTextColor = self.textLabel.highlightedTextColor; 
       self.titleLabel.textAlignment = UITextAlignmentLeft; 
       self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
         UIViewAutoresizingFlexibleBottomMargin; 
       [self.contentView addSubview:titleLabel]; 

       self.detailsLabel = [[[UILabel alloc] 
           initWithFrame:CGRectMake(cellRect.size.width - 
                  (detailsWidth + subdetailsWidth + rightMargin), 
                  y + 4, detailsWidth, titleHeight - 4)] autorelease]; 
       self.detailsLabel.font = [UIFont boldSystemFontOfSize:13.0f]; 
       self.detailsLabel.textColor = [UIColor colorWithRed:0.12 green:0.439 blue:0.847 alpha:1]; 
       self.detailsLabel.textAlignment = UITextAlignmentRight; 
       self.detailsLabel.highlightedTextColor = self.textLabel.highlightedTextColor; 
       self.detailsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | 
         UIViewAutoresizingFlexibleHeight; 
       [self.contentView addSubview:detailsLabel]; 

       self.subdetailsLabel = [[[UILabel alloc] 
           initWithFrame:CGRectMake(cellRect.size.width - (subdetailsWidth + rightMargin), 
                 y + 6, subdetailsWidth, titleHeight - 6)] autorelease]; 
       self.subdetailsLabel.font = [UIFont systemFontOfSize:11.0f]; 
       self.subdetailsLabel.textColor = [UIColor colorWithRed:0.12 green:0.439 blue:0.847 alpha:1]; 
       self.subdetailsLabel.textAlignment = UITextAlignmentCenter; 
       self.subdetailsLabel.highlightedTextColor = self.textLabel.highlightedTextColor; 
       self.subdetailsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | 
         UIViewAutoresizingFlexibleHeight; 
       [self.contentView addSubview:subdetailsLabel]; 

       self.subtitleLabel = [[[UILabel alloc] 
            initWithFrame:CGRectMake(x, y + titleHeight, width, subtitleHeight - 2)] autorelease]; 
       self.subtitleLabel.highlightedTextColor = self.textLabel.highlightedTextColor; 
       self.subtitleLabel.font = [UIFont systemFontOfSize:13.0f]; 
       self.subtitleLabel.textColor = [UIColor darkGrayColor]; 
       self.subtitleLabel.numberOfLines = 2; 
       self.subtitleLabel.lineBreakMode = UILineBreakModeTailTruncation; 
       self.subtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
         UIViewAutoresizingFlexibleTopMargin; 
       [self.contentView addSubview:subtitleLabel]; 

       self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
     return self; 
} 

- (id) init 
{ 
     return [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"RecentsCell"]; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
     [super setSelected:selected animated:animated]; 
} 

- (void) dealloc 
{ 
     self.titleLabel = nil; 
     self.subtitleLabel = nil; 
     self.detailsLabel = nil; 
     self.subdetailsLabel = nil; 
     [super dealloc]; 
} 

@end 

注意。這是我在表格視圖控制器中如何使用這個類。

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *cellIdentifier = @"TransactionCell"; 
     NSMutableArray *data = searching ? searchResult : dataSource; 
     NSDictionary *object = [data objectAtIndex:[indexPath row]]; 

     RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (cell == nil) { 
       cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                reuseIdentifier:cellIdentifier] autorelease]; 
     } 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
     cell.indexPath = indexPath; 
     cell.delegate = self; 

     cell.titleLabel.text = @"Transaction title"; 
     if (pendingTransaction) { 
       cell.titleLabel.textColor = [UIColor grayColor]; 
       cell.titleLabel.font = [UIFont systemFontOfSize:cell.titleLabel.font.pointSize]; 
     } else { 
       if (!isGreen) 
         cell.titleLabel.textColor = [UIColor colorWithRed:0.733 green:0.098 
                    blue:0.098 alpha:1]; 
       else 
         cell.titleLabel.textColor = cell.textLabel.textColor; 
       cell.titleLabel.font = [UIFont boldSystemFontOfSize:cell.titleLabel.font.pointSize]; 
     } 
     cell.subtitleLabel.text = @"Transaction subtitle"; 
     cell.detailsLabel.text = @"Some details like location"; 
     cell.subdetailsLabel.text = @"200 USD"; 
     return cell; 
} 

這將產生表非常相似,你在iphone電話應用中看到,就是標題,副標題,以藍色subdetails在小區的最右邊的位置等

你可以看到它在Torchoo appstore中的應用程序。它可以免費使用,您可以使用測試帳戶登錄並以此爲例。

希望這有助於一點。

0

你是否繼續添加子視圖到單元格不刪除任何子視圖?同時加入子視圖代碼 檢查條件不準確我已經寫:

if ([[cell.contentView subviews] count]==0) { 
    [self.contentView addSubview:imageView]; 
} 

如果單元格內容視圖不包含視圖,然後添加它。

+0

它不會總是返回計數> 0嗎?所以if子句永遠不會被執行。 – Mateusz

+0

我的工作大膽,像這樣的東西不完全是@Mateusz – Warewolf

0

以前的答案是正確的,雖然還有更多的問題可以解決。

添加子視圖的代碼部分只能在單元未出列時調用。否則,您將添加相同按鈕,視圖等的多個實例。

[cell addSubview:backImage]; 
[cell addSubview:detailTextLabel]; 
[cell addSubview:textLabel]; 
[cell addSubview:buyBtn]; 
[cell addSubview:img]; 
[cell addSubview:lbl]; 

而且我想最好是直接添加到cell.contentView而不是細胞。所以它應該看起來像這樣:

RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:cellIdentifier] autorelease]; 

    /*Create your subviews here*/ 

    [cell.contentView addSubview:backImage]; 
    [cell.contentView addSubview:detailTextLabel]; 
    [cell.contentView addSubview:textLabel]; 
    [cell.contentView addSubview:buyBtn]; 
    [cell.contentView addSubview:img]; 
    [cell.contentView addSubview:lbl]; 
} 

// Below just set properties of existing subviews of the cell: color, textColor, text etc. 
// You need to retrieve them and one way is to assign them to properties in 
// RecentsTableViewCell or set the tag property of the subviews and retrieve 
// them using e.g. [cell.contentView viewWithTag:kBuyButtonTag]. 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
    ... 
} 
else { 
    ... 
} 
+0

它已經解決了問題,但是一個礦石問題出現了。我有11個條目,但它只顯示7個,因爲它們在第一時間被查看,並且如果我向下滾動,它會複製前四個單元格並顯示它們。爲什麼? –

+0

操作系統正在重用單元(對象),因此您需要將它們正確地重新設置。如果一次只能看到7個單元格,則無論您的數據有多大(您有多少行)。操作系統將只啓動7個單元格,並在滾動時重複使用它們。 例如,假設每個單元在titleLabel中都有不同的文本。你需要每次在tableView:cellForRowAtIndexPath中設置這個屬性:根據傳遞的indexPath。如果沒有,之前設置的值將保持不變,您可能會看到來自其他indexPath的內容。 – Mateusz

+0

換句話說,當您滾動時,0(第1個單元格)處的單元格變得不可見,並且可以在7(第8個單元格)處重用它以顯示單元格。這可以節省內存和性能。 – Mateusz