0

所以我正在尝试做一些“简单”的事情,比如邮件应用程序,其中我的 UITableVieCell 子类告诉用户有多少项目属于该对象。所以一旦我抓住我的手机,我就会这样做:

if ([map.locations count] > 0) {
        UIView *locationsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LOCATIONS_VIEW_LENGTH, LOCATIONS_VIEW_LENGTH)]; // LOCATIONS_VIEW_LENGTH is 20.0
        locationsView.frame = CGRectMake((cell.contentView.frame.size.width - 45), (cell.contentView.frame.size.height - locationsView.frame.size.height) / 2, LOCATIONS_VIEW_LENGTH, LOCATIONS_VIEW_LENGTH);
        locationsView.layer.backgroundColor = [UIColor grayColor].CGColor;
        [locationsView.layer setCornerRadius:3];

        UIFont *arialFont = [UIFont systemFontOfSize:12.0];
        NSString *locationsText = [NSString stringWithFormat:@"%i", [map.locations count]];
        CGSize locationsLabelSize = [locationsText sizeWithFont:arialFont constrainedToSize:locationsView.frame.size lineBreakMode:UILineBreakModeTailTruncation];
        UILabel *numLocationsLabel = [[UILabel alloc] initWithFrame:CGRectMake((locationsView.frame.size.width - locationsLabelSize.width) / 2, (locationsView.frame.size.height - locationsLabelSize.height) / 2, locationsLabelSize.width, locationsLabelSize.height)];
        numLocationsLabel.text = locationsText;
        numLocationsLabel.textColor = [UIColor whiteColor];
        numLocationsLabel.font = arialFont;
        numLocationsLabel.backgroundColor = [UIColor clearColor];
        [locationsView addSubview:numLocationsLabel];

        [cell.contentView addSubview:locationsView];
    }

我最初尝试将此代码放在 UITableViewCell 的 awakeFromNib 方法中,但[map.locations count]即使在我的 configureCell 方法中它不是,我也总是小于 0。不知道为什么,无论如何,我在我的 cellForRowAtIndexPath 方法中调用 configureCell 并且它执行上述代码。它看起来非常可怕。

截图: 在此处输入图像描述

Apple 做了什么让他们的邮件变得这么好?谢谢!

4

1 回答 1

0

请参考simple-table-badges链接,因为它也包含源代码。希望有帮助。

另请参阅链接中的@RedBlueThing 答案

于 2012-08-28T05:23:22.793 回答