开发者

Problem in customize cell selection Style in UITableView in iPhone

In my application ,I am using Grouped style for TableView. In that I want to customize the cell selection Style.I want that selection Style to be red.

I am using the below code for that:

UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor];

[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];

By using the above code. I have a problem.Since I have tak开发者_Python百科en grouped Style table , in the selection of first and last rows, the selection is appear with sharp edged rectangle instead of appeaing round Corners.

Can Anyone Help me in this. Thanks in Advance.


Try this ,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground.png"]] autorelease];
    }

    // Cell configuration takes place here
}


add QuartzCore framework. and import QuartzCore/QuartzCore.h framework in .m file. and after that add following code in cellForRowAtIndexPath Method.

UIImageView *imv = [[UIImageView alloc]init];
imv.backgroundColor=[UIColor redColor];
[cell setSelectedBackgroundView:imv];
CALayer *layer = imv.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0];
[imv release];


Override -(void)setSelected:animated: in your subclass.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  if (selected)
    [self setBackgroundColor:[UIColor redColor]];
  else
    [self setBackgroundColor:[UIColor whiteColor]];
}

You can add fancy animation here to blend in and fade out the background upon selection and deselection if animated is YES.

In general UITableViewCell can be a bit awkward to subclass, be patient here.


 UIImageView *imageVieww=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell2.bounds.size.width, cell2.bounds.size.height)];


    imageVieww.image=[UIImage imageNamed:@"mavilik.png"];

     [cell2 setSelectedBackgroundView:imageVieww];

     [imageVieww release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜