开发者

Highlight the borders of the selected cell in UITableView

开发者 https://www.devze.com 2023-03-30 18:26 出处:网络
Is there any possibility to highlighting the borde开发者_Python百科rs of the selected cell in a UITableViewController in Objective-c?You can try to make an custom UIView/UIImageView for the selection

Is there any possibility to highlighting the borde开发者_Python百科rs of the selected cell in a UITableViewController in Objective-c?


You can try to make an custom UIView/UIImageView for the selection with setSelectedBackgroundView:

Here is a example code I use for custom gradient in a custom tableviewcell:

UIView *selctionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = selctionView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor], (id)[[UIColor redColor] CGColor], nil];

[selctionView.layer insertSublayer:gradient atIndex:0];

[self setSelectedBackgroundView:selctionView];

EDIT:

I found out that you also can use the methods:

[test.layer setBorderColor: [[UIColor redColor] CGColor]];
[test.layer setBorderWidth: 1.0]; 

for the layer.

be sure to import QuartzCore.h

else for the whole tableview:

[tableViewController.tableView setSeparatorColor:[UIColor redColor]];


This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method. You must not set the color in the cellForRowAtIndexPath.

This works for both the plain and grouped style :

Code:

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor redColor]; }

P.S: Here the documentation extract for willDisplayCell :

"A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out."

I've found this information in this post from colionel. Thank him!

0

精彩评论

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