使用自定义UIView的Tableview,添加标签重叠

时间:2013-07-11 14:28:54

标签: ios objective-c uitableview uiview uilabel

我正在尝试实现类似功能的ibooks书架,我使用GSBookShelf非常简单直接,它将按钮添加为单元格并且效果很好。

我想为每个按钮添加一些标签,我成功但偶尔会说5个标签中有2个标签与之前的按钮标签重叠。

通常是这样的 normally like this

有时会发生这种情况: Sometime this happens

代码:

- (UIView *)bookShelfView:(GSBookShelfView *)bookShelfView bookViewAtIndex:(NSInteger)index {

    NSDictionary *currentArticle = [_bookArray objectAtIndex:index];

    static NSString *identifier = @"bookView";
    MyBookView *bookView = (MyBookView *)[bookShelfView dequeueReuseableBookViewWithIdentifier:identifier];
    if (bookView == nil) {
        bookView = [[MyBookView alloc] initWithFrame:CGRectZero];
        bookView.reuseIdentifier = identifier;
        [bookView addTarget:self action:@selector(bookViewClicked:) forControlEvents:UIControlEventTouchUpInside];

    }
    [bookView setIndex:index];
    [bookView setSelected:[(NSNumber *)[_bookStatus objectAtIndex:index] intValue]];
    //[bookView setFileName:[currentArticle objectForKey:@"name"] ];
    //[bookView setFileName:[currentArticle objectForKey:@"name"] :index];

    UIImage *image = nil;
    image = [self returnCellImagefor:[currentArticle objectForKey:@"imagename"]];
    [bookView setBackgroundImage:image forState:UIControlStateNormal];

    UILabel *_fileLabel=nil;
    _fileLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 180, 200, 46)];
    _fileLabel.text=[currentArticle objectForKey:@"name"];
    _fileLabel.textColor=[UIColor whiteColor];
    _fileLabel.backgroundColor=[UIColor clearColor];
    _fileLabel.font = [UIFont fontWithName:@"Gill Sans" size:16];
    //_fileLabel.textAlignment=UITextAlignmentCenter;
    _fileLabel.textAlignment = NSTextAlignmentCenter;
    _fileLabel.numberOfLines=0;
    _fileLabel.lineBreakMode = UILineBreakModeWordWrap;
    [_fileLabel sizeToFit];
    [bookView addSubview:_fileLabel];

    return bookView;
}

图像很好,它们从不重叠,但标签有时会这样做。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

你把bookShelfView功能放在哪里?显示您的TableView代码。我打赌你遇到的问题是,当你向下滚动表bookShelfView再次被调用时。您的标签正在为两个不同的值创建两次。

这是你看到重叠发生的唯一方式,这是一次又一次地启动

_fileLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 180, 200, 46)];

我会查看你的TableView代码。我以前曾问过类似的问题。 (不确切)。查看UITableView and UILabel repeating