为UITableView的第一个和最后一个单元格创建圆角

时间:2012-10-26 17:34:24

标签: iphone ios uitableview

我想为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

2 个答案:

答案 0 :(得分:4)

只需将您的UITableView初始化样式更改为Grouped,就像那样:

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

这将使您的第一个单元格从顶部倒圆,最后一个单元格从底部倒圆。

在不使用UITableViewController初始化程序的情况下编辑:

@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

答案 1 :(得分:2)

假设您将此代码放在tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,您可以这样做:

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