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

你可能感兴趣的:(CollectionViewCell 自定义button,点击button改变对应button状态)