【发布时间】: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