2

我想为 UITableView 的第一个和最后一个单元格制作圆角。我从这篇文章自定义圆角中看到,您可以在 xib 文件中使用自定义单元格,设置它们的唯一标识符,如:@“beginCell”、@“middleCell”、@“endCell”。我不想使用自定义单元格。有没有其他方法可以做到这一点?

例如:

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

像这样的东西。但是没有称为计数的属性。有没有其他财产?

编辑:

视图控制器.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
4

2 回答 2

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
于 2012-10-26T17:50:10.357 回答
2

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

if (indexPath.row == 0 || indexPath.row == ([indexPath length]-1))
{
   cell.layer.cornerRadius = 10;
}
于 2012-10-26T17:39:15.893 回答