0

也许这是一个非常简单的答案,但我到处搜索,似乎每个人都试图做与我想做的相反的事情。我有一个自定义 UITableViewCell 定义如下。

#import "TagCell.h"
#import "TagRepository.h"
#import "TagsViewController.h"
@implementation TagCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self setFrame:CGRectMake(0, 1, self.frame.size.width, 37)];
        self.selectionStyle= UITableViewCellSelectionStyleNone;
        self.textLabel.hidden=NO;
        [self.textLabel setFrame:CGRectMake(self.bounds.size.width/2-40, self.bounds.origin.y+1, 300, 30)];
        self.textLabel.text= @"test";
        [self.textLabel setFont:[UIFont fontWithName:@"Helvetics" size:30] ];

    }
    return self;
}
-(void) layoutSubviews
{

    NSArray *viewsToRemove = [self subviews];
    for (UIView *v in viewsToRemove) {
        if([v isKindOfClass:[UIButton class]])
        {
            [v removeFromSuperview]; //used to remove and redraw the button during a orientation change
        }
    }
   UIButton * removeButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [removeButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:18] ];
    [removeButton setFrame:CGRectMake(7*self.bounds.size.width/8, self.bounds.origin.y+3, 30, 30)];
    removeButton.hidden=NO;
    [removeButton addTarget:self action:@selector(removeTag) forControlEvents:UIControlEventTouchUpInside];
    [removeButton addTarget:self action:@selector(removeTag) forControlEvents:UIControlEventTouchDragInside];
    [removeButton setBackgroundImage:[UIImage imageNamed:@"button_red_unselect.png"] forState:UIControlStateNormal];
    [removeButton setBackgroundImage:[UIImage imageNamed:@"button_red_select.png"] forState:UIControlStateSelected];
    [removeButton setImage:[UIImage imageNamed:@"icon_minus.png"] forState:UIControlStateNormal];
    [removeButton setImage:[UIImage imageNamed:@"icon_minus.png"] forState:UIControlStateSelected];

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

    // Configure the view for the selected state
}
-(void) removeTag
{
    for( int i=0 ; i< [TagRepository instance].tags.count; i++)
    {
            if([self.textLabel.text isEqualToString:[TagRepository instance].tags[i]])
            {
                [[TagRepository instance].tags removeObjectAtIndex:i] ;
            }
    }
    _reloadTable();
   // [_delegate.table reloadData];
    //RELOAD TABLE
}

@end

所以发生的事情是,当我打开表格视图时,当有一个活动的自定义单元格时,边框不存在,它看起来好像后面有一个空白单元格..或其他东西。

这是不希望的,我希望单元格周围有一个边框。包含该表的视图设置了 heightforcellatindex:indexpath = 40,因此委托函数返回 40 作为单元格高度值,我认为问题出在此与自定义单元格中的子视图之间的某种不匹配之间。你看这有什么不对吗??

请注意,我已经更改了 Helvetica 中的错字。

4

1 回答 1

0

如果将 [super layoutSubviews] 添加到 layoutSubviews 方法,将出现单元格分隔符。我不确定你后面的空白单元格是什么意思——当我尝试你的自定义单元格时,我没有看到类似的东西。此外,您应该将按钮添加(和删除)到单元格的 contentView,而不是单元格本身。

于 2013-05-04T04:15:38.330 回答