【问题标题】:UITableViewCell drawInRect iOS7UITableViewCell drawInRect iOS7
【发布时间】:2013-09-19 22:24:56
【问题描述】:

您好,我正在尝试使用以下代码在 iOS 7 中的 UITableViewCell 中绘制字符串

-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
CGRect playerNameRect = CGRectMake(0, kCellY, kPlayerNameSpace, kCellHeight);

NSDictionary*dictonary = [NSDictionary
                          dictionaryWithObjectsAndKeys:
                          [UIColor hmDarkGreyColor], NSForegroundColorAttributeName,
                          kFont, NSFontAttributeName,
                          nil];

[self.playerName drawInRect:playerNameRect withAttributes:dictonary];

}

但是我什么都看不到... self.playerName 不为零,并且 playerNameRect 是正确的。

我以前使用以下代码来做同样的事情,但最近在 iOS 7 中被弃用了

        [self.playerName drawInRect:playerNameRect withFont:kFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];

同样奇怪的是,我无法在 UITableViewCell 上的 drawRect 中绘制任何东西......当我在 UIView 上绘制矩形时,不推荐使用的代码有效。

【问题讨论】:

    标签: objective-c uitableview ios7 drawrect


    【解决方案1】:

    您不应该使用UITableViewCelldrawRect 方法来执行自定义绘图。正确的方法是创建一个自定义 UIView 并将其添加为单元格的子视图(作为 contentView 属性的子视图)。您可以将绘图代码添加到此自定义视图中,一切都会正常工作。

    希望这会有所帮助!

    也看看这些帖子:

    Table View Cell custom drawing 1

    Table View Cell custom drawing 2

    Table View Cell custom drawing 3

    【讨论】:

    • 很酷的人,谢谢...这只是 iOS7 中最近的东西,没有在 UITableViewCell 的 drawRect 中绘制吗?
    • 不客气。这对 iOS 7 来说并不新鲜。不知道是什么时候介绍的,但我想说一直都是这样。
    • 在 iOS 7 中使用 UIView 作为 contentView 时,表格的插图在左侧或右侧创建空间。您需要在 VC 的 viewWillLayoutSubviews 中设置表格的插图,如下所示:“self .yourTable.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);"
    【解决方案2】:

    正如其他人所说,不要直接使用 UITableViewCell 的 drawRect 选择器。通过这样做,您依赖于 UITableViewCell 的实现细节,Apple 不保证这种行为不会在未来的版本中中断,就像它在 iOS 7 中所做的那样......相反,创建一个自定义 UIView 子类,并添加它作为 UITableViewCell 的 contentView 的子视图,如下所示:

    @implementation CustomTableViewCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            [self.contentView addSubview:[[CustomContentView alloc]initWithFrame:self.contentView.bounds]];
        }
        return self;
    }
    
    @end
    

    还有 CustomContentView:

    @implementation CustomContentView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
        }
        return self;
    }
    
    
    - (void)drawRect:(CGRect)rect
    {
        NSDictionary * attributes = @{
                                      NSFontAttributeName : [UIFont fontWithName:@"Helvetica-bold" size:12],
                                      NSForegroundColorAttributeName : [UIColor blackColor]
                                      };
    
        [@"I <3 iOS 7" drawInRect:rect withAttributes:attributes];
    }
    
    @end
    

    像魅力一样工作!

    【讨论】:

      【解决方案3】:

      尝试在初始化中设置cell.backgroundColor = [UIColor clearColor]

      【讨论】:

        【解决方案4】:

        虽然我同意接受的答案,但以下是我对它的记录:

        如果您不需要任何内置的 UITableViewCell 功能(滑动、删除、重新排序等),而只是将其用作绘制自定义内容的容器,那么您可能需要考虑删除所有单元格tableview:willDisplayCell:ForRowAtIndexPath 中的子视图。这将使您的绘图再次可见并获得最佳性能(因为您摆脱了不需要的子视图)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-26
          • 1970-01-01
          • 1970-01-01
          • 2014-02-15
          • 1970-01-01
          • 1970-01-01
          • 2013-09-26
          • 2013-09-18
          相关资源
          最近更新 更多