2013-01-03 57 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