設定cell預設選中
阿新 • • 發佈:2019-02-20
1.UITableView多選時設定預設選中幾個cell
2.UICollectionView 設定預設cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.multipleSelectionBackgroundView = [[UIView alloc] initWithFrame:cell.bounds]; cell.multipleSelectionBackgroundView.backgroundColor = [UIColor clearColor]; cell.selectionStyle = UITableViewCellSelectionStyleDefault; //預設選中上次選中的cell for (NSIndexPath *index in self.indexsArr) { if (index == indexPath) { [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:(UITableViewScrollPositionNone)]; break; } } return cell; }
[self.view addSubview:self.collectionView]; //預設選中 for (NSIndexPath *index in self.indexArr) { [self.collectionView selectItemAtIndexPath:index animated:YES scrollPosition:(UICollectionViewScrollPositionNone)]; }
collectonView有點奇怪,放在初始化cell的代理方法裡面設定預設選中沒有任何效果,在collectionView有了資料以後並且載入在檢視上再設定預設cell成功。- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ...... cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds]; cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:Color_Blue]; return cell; }