在滚动幻灯片中的每个 UICollectionViewCell 中弄乱 UIButton [英] Messing UIButton in each UICollectionViewCell in Scrolling Filmstrip

查看:18
本文介绍了在滚动幻灯片中的每个 UICollectionViewCell 中弄乱 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的

解决方案

通过为每个数据源 dict 添加一个键,在您的数据源中维护要选择的单元格.例如:@"isSelected"

然后在 cellForRowAtIndexPath:

if ([[cellData valueForKey:@"isSelected"] boolValue]) {[selectButton setSelected:NO];} 别的 {[selectButton setSelected:YES];}

并在您的 -(void) buttonPressed:(UIButton *)sender 方法中:

CGPoint point = [self.collectionView convertPoint:CGPointZero fromView:sender];NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];if ([[cellData valueForKey:@"isSelected"] boolValue]) {[cellData setValue:@"false" forKey:@"isSelected"];} 别的 {[cellData setValue:@"true" forKey:@"isSelected"];}[self.framesCollectionView reloadItemsAtIndexPaths:@[indexPath]];

AND

在您的 cellForRowAtIndexPath 中为 UIButton 设置一个标签.然后在将 UIButton 作为子视图添加到单元格的 contentView 之前检查是否已存在带有此标记的视图.如果单元格已被重用,只需使用现有按钮即可.

This is a follow-up question to my previous one and this time I have a problem with UIButton that I have added in each UICollectionViewCell. Before diving into the problem, let me brief out all I've got so far.

Here's the basic rundown of how my UICollectionView in Scrolling Filmstrip style within UITableView works:

  • Create a normal UITableView with a custom UITableViewCell
  • Create a custom UIView that will be added to the cell's contentView
  • The custom UIView will contain a UICollectionView
  • The custom UIView will be the datasource and delegate for the UICollectionView and manage the flow layout of the UICollectionView
  • Use a custom UICollectionViewCell to handle the collection view data
  • Use NSNotification to notify the master controller's UITableView when a collection view cell has been selected and load the detail view.

So far, I've been able to add UIButton in each UICollectionViewCell and when I tap the UIButton its image will change to checked mark but the problem occurs when I scroll up/down. Once the cell with checked mark UIButton is off-screen and scrolled back on-screen again, UIButton image starts to get messed around. For example, when UIButton image in cell1 should be checked mark but it's not. Instead checked mark will be appeared in another cell.

Here below is my relevant code:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PostsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PostsCollectionViewCell" forIndexPath:indexPath];
    NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];

    cell.postTextLabel.text = [cellData objectForKey:@"text"];
    cell.locationNameLabel.text = [cellData objectForKey:@"locationName"];

    //  >>> Select Button <<<
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectButton setFrame:CGRectMake(110, 150, 20, 20)];
    [selectButton setImage:[UIImage imageNamed:@"uncheckedDot.png"] forState:UIControlStateNormal];
    [selectButton setImage:[UIImage imageNamed:@"checkedDot.png"] forState:UIControlStateSelected];
    [cell.contentView addSubview:selectButton];
    [selectButton addTarget:self
                   action:@selector(buttonPressed:)
         forControlEvents:UIControlEventTouchUpInside];
    //  >>> End Select Button <<<<

    return cell;
}

//  >>> Select Button Method <<<
-(void) buttonPressed:(UIButton *)sender
{
    if([sender isSelected]){
        //...
        [sender setSelected:NO];
    } else {
        //...
        [sender setSelected:YES];
    }
}

Any help would be greatly appreciated! Thanks in advance.

解决方案

Maintain in your Data source which cell is to be selected by adding a key for each data source dict. For example:@"isSelected"

Then in cellForRowAtIndexPath:

if ([[cellData valueForKey:@"isSelected"] boolValue]) {
    [selectButton setSelected:NO];
} else {
    [selectButton setSelected:YES];
}

and in your -(void) buttonPressed:(UIButton *)sender Method:

CGPoint point = [self.collectionView convertPoint:CGPointZero fromView:sender];
NSIndexPath  *indexPath = [self.collectionView indexPathForItemAtPoint:point];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];

if ([[cellData valueForKey:@"isSelected"] boolValue]) {
     [cellData setValue:@"false" forKey:@"isSelected"];
} else {
    [cellData setValue:@"true" forKey:@"isSelected"];
}

[self.framesCollectionView reloadItemsAtIndexPaths:@[indexPath]];

AND

In your cellForRowAtIndexPath set a tag for the UIButton. Then before adding a UIButton as subview to the cell's contentView check if a view with this tag already exists. In case the cell has been reused, just use the already existing button.

这篇关于在滚动幻灯片中的每个 UICollectionViewCell 中弄乱 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆