我想为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
3条答案
按热度按时间sc4hvdpw1#
只需将
UITableView
初始化样式更改为Grouped,如下所示:这将使您的第一个单元格从顶部四舍五入,最后一个单元格从底部四舍五入。
不使用UITableViewController初始化器时,EDIT:
kdfy810k2#
假设你把这段代码放在
tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中,你可以这样做:c9qzyr3d3#
要使用Swift修圆表格中第一个单元格的顶角和最后一个单元格的底角,可以利用UIBezierPath类为表格单元格创建自定义形状。下面是一个如何实现此目标的示例:
在此示例中,RoundedCornerTableViewController类子类UITableViewController用于显示表视图。在tableView(_:cellForRowAt:)方法中,我们创建一个UIBezierPath来定义每个单元格的圆角形状。
对于第一个单元格,我们使用.topLeft和.topRight角选项来圆化顶角。对于最后一个单元格,我们使用.bottomLeft和.bottomRight角选项来圆整底部的角。其他单元格没有圆角,我们使用规则的矩形UIBezierPath。
我们创建一个CAShapeLayer,并将其路径设置为为每个单元格创建的UIBezierPath。最后,我们将形状层作为模板应用于细胞层,这给出了细胞圆角的外观。
确保在故事板中将视图控制器的类设置为RoundedCornerTableViewController,或者以编程方式创建示例。
利用该实施方式,第一单元将具有倒圆的顶角,最后一单元将具有倒圆的底角,并且其它单元将不具有倒圆的顶角。