【问题标题】:UITableView Shadow not coming properly (mis aligned)UITableView 阴影未正确出现(未对齐)
【发布时间】:2023-03-29 06:35:01
【问题描述】:

我正在尝试将底部阴影添加到 UITableViewCell 并且它即将到来但未对齐。这是我的代码,我也上传了我的截图

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    if(indexPath.row==0){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell1"]];
    // Configure the cell...
    }else if(indexPath.row==1){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell2"]];
    }else if(indexPath.row==2){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell3"]];

    }else if(indexPath.row==3){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell4"]];

    }else {
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell5"]];

    }
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOpacity = 0.5;
    cell.layer.shadowOffset = CGSizeMake(0, 1);
    return cell;
}

截图

如何正确对齐底部的阴影?

【问题讨论】:

  • 设置到单元格的图像有问题,检查它的大小和单元格的尺寸。
  • 你能解释一下吗?
  • 你能看到图像在某些单元格中重复了 2 次吗? UIColor's->colorWithPatternImage 会明显重复。
  • 哦....那么我怎样才能添加为背景?
  • 尝试将 UIImageView 添加到单元格或使用单元格的 backgroundView 或 UITableViewCell 的 selectedBackgroundVIew 属性。

标签: ios objective-c uitableview shadow


【解决方案1】:

请用这个更新你的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;

if(indexPath.row==0){

// Configure the cell...
    av.image = [UIImage imageNamed:@"cell1"];
    cell.backgroundView = av;
}else if(indexPath.row==1){
    av.image = [UIImage imageNamed:@"cell2"];
    cell.backgroundView = av;
}else if(indexPath.row==2){
    av.image = [UIImage imageNamed:@"cell3"];
    cell.backgroundView = av;
}else if(indexPath.row==3){
    av.image = [UIImage imageNamed:@"cell4"];
    cell.backgroundView = av;
}else {
    av.image = [UIImage imageNamed:@"cell5"];
    cell.backgroundView = av;
}

return cell;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    相关资源
    最近更新 更多