我有一个名为 GameCell 的自定义单元类,UI 在 Storyboard 中创建。当我的单元格加载时,它们会叠加加载。 cell.clipsToBounds YES 和 NO 会出现此问题,只是变化不同:
我也试过 [cell setClipsToBounds:(BOOL)] 但没有成功。
这是我的 cellForRowAtIndexPath 方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"gameCell";
GameCell *cell = (GameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView.superview setClipsToBounds:NO];
NSLog(@"made a cell");
PFObject *myPartners = [self.games objectAtIndex:indexPath.row];
PFUser *partner = [self.partnerList objectAtIndex:indexPath.row];
// Cell profile image
cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.width / 2;
cell.profileImage.clipsToBounds = YES;
if([partner objectForKey:@"profilePic"]!=nil){
cell.profileImage.image = [partner objectForKey:@"profilePic"];
}
else {
cell.profileImage.image = [UIImage imageNamed:@"smile_green.png"];
}
//Cell indicators
if((int)[myPartners objectForKey:@"sent"]==1){
cell.myIndicator.image = [UIImage imageNamed:@"arrow_icon_dbl.png"];
}
else if ([myPartners objectForKey:@"sent"]==0){
cell.myIndicator.image = [UIImage imageNamed:@"arrow_icon.png"];
}
cell.partnerName.text = [myPartners objectForKey:@"receiverName"];
cell.gameId = myPartners.objectId;
// Cell drop shadow
[cell.cellView.layer setShadowColor:[UIColor blackColor].CGColor];
[cell.cellView.layer setShadowOpacity:0.5];
[cell.cellView.layer setShadowRadius:2.0];
[cell.cellView.layer setShadowOffset:CGSizeMake(1.0, 1.0)];
// Cell buttons
if([myPartners objectForKey:@"picture"]!=nil) {
[cell.myPlay setEnabled:NO];
} else {
[cell.myPlay setEnabled:YES];
}
return cell;
}
View 是嵌入在 NavigationController 中的 UITableViewController
最佳答案
你已经使用了dequeueReusableCellWithIdentifier:
像这样使用
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
并查看此链接。
UITableView cell Contents are disappearing and overlapping when scrolled in UITableViewcell?
关于ios - 自定义单元格在 UITableView 中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25496931/