2

UITableViewの最初と最後のセルの角を丸くしたいと思います。この投稿から、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
4

2 に答える 2

4

UITableView初期化スタイルを次のようにグループ化に変更するだけです。

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 に答える