1.UITableView多选时设置默认选中几个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;
}
2.UICollectionView 设置默认cell
[self.view addSubview:self.collectionView];
//默认选中
for (NSIndexPath *index in self.indexArr) {
[self.collectionView selectItemAtIndexPath:index animated:YES scrollPosition:(UICollectionViewScrollPositionNone)];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:Color_Blue];
return cell;
}
collectonView有点奇怪,放在初始化cell的代理方法里面设置默认选中没有任何效果,在collectionView有了数据以后并且加载在视图上再设置默认cell成功。