CollectionViewCell 自定义button,点击button改变对应button状态

UITableViewCell cell中创建UICollectionView
//UITableViewCell 赋值
if (self.provinceArray.count) {

        for (NSDictionary *dataDic in self.provinceArray) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
           //设置第一个为选择状态
            [dic setObject:@"0" forKey:@"selected"];
            [cell.dataArray addObject:dic];
        }
        [[cell.dataArray objectAtIndex:0] setObject:@"1" forKey:@"selected"];
    }

//UICollectionViewCell 复用方法中

  • (UICollectionViewCell )collectionView:(UICollectionView)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dataDic = [self.dataArray objectAtIndex:indexPath.item];
    CJWProvinceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CJWProvinceCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    [cell.provinceBtn setTitle:emptyString([dataDic objectForKey:@"name"]) forState:UIControlStateNormal];
    if ([[dataDic objectForKey:@"selected"] isEqualToString:@"1"]) {
    cell.provinceBtn.selected = YES;
    }else{
    cell.provinceBtn.selected = NO;
    }
    return cell;
    }

//点击UICollectionViewCell的方法

  • (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    for (int i = 0;i<self.dataArray.count; i++) {
    NSDictionary *dic = self.dataArray[i];
    [dic setValue:@"0" forKey:@"selected"];
    }
    NSDictionary *dic = self.dataArray[indexPath.item];
    [dic setValue:@"1" forKey:@"selected"];
    NSString *provinceId = emptyString([[self.dataArray objectAtIndex:indexPath.item] objectForKey:@"code"]);

    if (_delegate && [_delegate respondsToSelector:@selector(selectedProvinceRequestFeatureList:)]) {
    [_delegate selectedProvinceRequestFeatureList:provinceId];
    }
    [self.collectionView reloadData];
    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容