简体   繁体   中英

Making rounded corners for the first and last cell of UITableView

I would like to make rounded corners for the first and last cell of UITableView. I have seen from this post Custom Rounded corners that you can use custom cells in a xib file, set their unique identifiers like: @"beginCell", @"middleCell", @"endCell". I do not want to use custom cells. Is there any other way to do this?

For example:

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

Something like this. But there is no property called count. Is there any other property?

EDITED:

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

Just change your UITableView initialization style to Grouped like that:

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

This will make your first cell rounded from top and last cell rounded from bottom.

EDIT in case of not using UITableViewController initializer:

@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

Assuming you're putting this code in tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , you can do it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM