ios 为UITableView的第一个和最后一个单元格制作圆角

ruarlubt  于 2023-06-25  发布在  iOS
关注(0)|答案(3)|浏览(146)

我想为UITableView的第一个和最后一个单元格制作圆角。我从这篇文章Custom Rounded corners中看到,你可以在xib文件中使用自定义单元格,设置它们的唯一标识符,比如:@“beginCell”、@“middleCell”、@“endCell”。我不想使用自定义单元格。还有别的办法吗
例如:

if (cell.count == 0 or cell.count == last)
{
   cell.layer.cornerRadius = 10;
}

就像这样但是没有一个属性叫做count。还有其他财产吗?

已编辑:

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tableItems,images;
- (void)viewDidLoad
{
    [super viewDidLoad];
    tableItems = [[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3",nil];
    images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"clock.png"],[UIImage imageNamed:@"eye.png"],[UIImage imageNamed:@"target.png"],nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Required Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    { 
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        UIView *v = [[UIView alloc] init];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
        //changing the radius of the corners
        //cell.layer.cornerRadius = 10;

    }

    //Set the image in the row
    cell.imageView.image = [images objectAtIndex:indexPath.row];

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}
#pragma mark - Optional Methods
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.backgroundColor = [ UIColor greenColor];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    NSString *selectedRow = [tableItems objectAtIndex:indexPath.row];

    secondViewController.selectedRow = selectedRow;

    //[self.navigationController pushViewController:secondViewController animated:YES];
    [self presentViewController:secondViewController animated:YES completion:nil];

    [secondViewController printRowNumber:indexPath.row];
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    return self;
}
@end
sc4hvdpw

sc4hvdpw1#

只需将UITableView初始化样式更改为Grouped,如下所示:

yourTableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];

这将使您的第一个单元格从顶部四舍五入,最后一个单元格从底部四舍五入。

不使用UITableViewController初始化器时,EDIT:

@interface YourViewController

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation YourViewController

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super init])
    {
        ...
        tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
        ...
        [self.view addSubview:tableView];
    }
    return self;
}

@end
kdfy810k

kdfy810k2#

假设你把这段代码放在tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,你可以这样做:

if (indexPath.row == 0 || indexPath.row == ([indexPath length]-1))
{
   cell.layer.cornerRadius = 10;
}
c9qzyr3d

c9qzyr3d3#

要使用Swift修圆表格中第一个单元格的顶角和最后一个单元格的底角,可以利用UIBezierPath类为表格单元格创建自定义形状。下面是一个如何实现此目标的示例:

import UIKit

class RoundedCornerTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the table view background color to clear
        tableView.backgroundColor = UIColor.clear

        // Register the table view cell
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        
        // Set the cell background color to white
        cell.backgroundColor = UIColor.white

        // Create a UIBezierPath for the rounded corners
        let maskPath: UIBezierPath
        if indexPath.row == 0 {
            // First cell, round the top corners
            maskPath = UIBezierPath(roundedRect: cell.bounds,
                                    byRoundingCorners: [.topLeft, .topRight],
                                    cornerRadii: CGSize(width: 10, height: 10))
        } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
            // Last cell, round the bottom corners
            maskPath = UIBezierPath(roundedRect: cell.bounds,
                                    byRoundingCorners: [.bottomLeft, .bottomRight],
                                    cornerRadii: CGSize(width: 10, height: 10))
        } else {
            // Other cells, no rounded corners
            maskPath = UIBezierPath(rect: cell.bounds)
        }

        // Create a CAShapeLayer with the path
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = maskPath.cgPath

        // Apply the mask to the cell's layer
        cell.layer.mask = shapeLayer

        return cell
    }

}

在此示例中,RoundedCornerTableViewController类子类UITableViewController用于显示表视图。在tableView(_:cellForRowAt:)方法中,我们创建一个UIBezierPath来定义每个单元格的圆角形状。
对于第一个单元格,我们使用.topLeft和.topRight角选项来圆化顶角。对于最后一个单元格,我们使用.bottomLeft和.bottomRight角选项来圆整底部的角。其他单元格没有圆角,我们使用规则的矩形UIBezierPath。
我们创建一个CAShapeLayer,并将其路径设置为为每个单元格创建的UIBezierPath。最后,我们将形状层作为模板应用于细胞层,这给出了细胞圆角的外观。
确保在故事板中将视图控制器的类设置为RoundedCornerTableViewController,或者以编程方式创建示例。
利用该实施方式,第一单元将具有倒圆的顶角,最后一单元将具有倒圆的底角,并且其它单元将不具有倒圆的顶角。

相关问题