Problem setting style cell selection in UITableView on iPhone

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

I use the following code for this:

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

Using the code above. I have a problem. Since I took the grouped stylesheet into the selection of the first and last lines, the selection appears with the sharp edge of the rectangle instead of turning around the Corners.

Can anyone help me on this. Thanks at Advance.

+6
source share
4 answers

Try it,

 - (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 } 
+3
source

add the QuartzCore framework. and import the QuartzCore / QuartzCore.h wireframe into the .m file. and after that add the following code to the 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]; 
+1
source

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 fantastic animation here to mix and remove the background when selecting and deselecting if animated is YES.

In general, a UITableViewCell may be a little inconvenient for a subclass, be patient here.

0
source
  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]; 
-2
source

Source: https://habr.com/ru/post/891340/


All Articles