美文网首页
CollectionViewCell 自定义button,点击b

CollectionViewCell 自定义button,点击b

作者: Forever3389 | 来源:发表于2018-05-02 14:18 被阅读0次

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

    相关文章

      网友评论

          本文标题:CollectionViewCell 自定义button,点击b

          本文链接:https://www.haomeiwen.com/subject/owccrftx.html