0

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

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

通常是这样的 通常是这样的

有时会发生这种情况: 有时会发生这种情况

代码:

- (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;
}

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

知道为什么会这样吗?

4

1 回答 1

1

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

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

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

我会检查您的 TableView 代码。我之前也问过类似的问题。(不准确)。看看UITableView 和 UILabel 重复

于 2013-07-11T16:30:46.617 回答