132 Set the background color of the selected cell (expand Knowledge: Set the background picture of the selected cell)

Source: Internet
Author: User

The effect is as follows:

ViewController.h

1 #import <UIKit/UIKit.h>23@interface  viewcontroller: Uitableviewcontroller4 @property (Strong, Nonatomic) Nsarray *Arrdatasource; 5 @property (Strong, Nonatomic) Nsarray *Arrselectionstyle; 6 7 @end

Viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One      A [self layoutui]; - } -  the- (void) didreceivememorywarning { - [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. - } +  -- (void) Layoutui { +_arrdatasource = @[@"Uitableviewcellselectionstylenone", A                        @"Uitableviewcellselectionstyleblue (fail to be used Cell.selectedbackgroundview custom)", at                        @"Uitableviewcellselectionstylegray", -                        @"Uitableviewcellselectionstyledefault (default; iOS7 valid, self-defined is gray)", -                        @"Customize the background color of selected cells", -                        @"Customize the background picture of a selected cell"]; -      -_arrselectionstyle =@[[nsnumber Numberwithinteger:uitableviewcellselectionstylenone], in [NSNumber Numberwithinteger:uitableviewcellselectionstyleblue], - [NSNumber Numberwithinteger:uitableviewcellselectionstylegray], to [NSNumber Numberwithinteger:uitableviewcellselectionstyledefault], +[NSNumber Numberwithinteger:4], -[NSNumber Numberwithinteger:5]]; the      *Self.navigationItem.title =@"sets the background color of the selected cell"; $ }Panax Notoginseng  - #pragmaMark-tableview the-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section { +     return @"Selectionstyle List"; A } the  +-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView { -     return 1; $ } $  --(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section { -     return_arrdatasource.count; the } - Wuyi-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { the     StaticNSString *cellidentifier =@"Cellidentifier"; -UITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; Wu     if(!cell) { -Cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; About     } $      -Nsinteger row =Indexpath.row; -Cell.textLabel.text =_arrdatasource[row]; -Cell.textLabel.numberOfLines =2; A      +UIImage *img =Nil; the     //sets the background color or background picture of the selected cell -     Switch(row) { $          Case 0: the          Case 2: the          Case 3: theCell.selectionstyle =[_arrselectionstyle[row] integervalue]; the              Break; -          Case 1: in          Case 4: theCell.selectedbackgroundview =[[UIView alloc] init]; theCell.selectedBackgroundView.backgroundColor = [Uicolor Bluecolor];//the corresponding Cell.backgroundcolor About              Break; the          Case 5: theIMG = [UIImage imagenamed:@"Frame"]; theIMG = [img stretchableimagewithleftcapwidth: -Topcapheight: -]; +Cell.selectedbackgroundview = [[Uiimageview alloc] initwithimage:img];//the corresponding Cell.backgroundview -              Break; the     }Bayi     returncell; the      the     /* - typedef ns_enum (Nsinteger, Uitableviewcellselectionstyle) { - Uitableviewcellselectionstylenone, the Uitableviewcellselectionstyleblue, the Uitableviewcellselectionstylegray, the uitableviewcellselectionstyledefault Ns_enum_available_ios (7_0) the      }; -      */ the } the  the- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {94      the } the  the @end

AppDelegate.h

1 #import <UIKit/UIKit.h>23@interface Appdelegate:uiresponder < Uiapplicationdelegate>4 @property (Strong, Nonatomic) UIWindow *window; 5 @property (Strong, Nonatomic) Uinavigationcontroller *Navigationcontroller; 6 7 @end

Appdelegate.m

1 #import "AppDelegate.h"2 #import "ViewController.h"3 4 @interfaceappdelegate ()5 @end6 7 @implementationappdelegate8 9-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Ten_window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; OneViewcontroller *viewcontroller =[[Viewcontroller alloc] init]; A_navigationcontroller =[[Uinavigationcontroller alloc] initwithrootviewcontroller:viewcontroller]; -_window.rootviewcontroller =_navigationcontroller; -     //[_window Addsubview:_navigationcontroller.view];//when _window.rootviewcontroller is associated, this sentence is optional the [_window makekeyandvisible]; -     returnYES; - } -  +- (void) Applicationwillresignactive: (UIApplication *) Application { - } +  A- (void) Applicationdidenterbackground: (UIApplication *) Application { at } -  -- (void) Applicationwillenterforeground: (UIApplication *) Application { - } -  -- (void) Applicationdidbecomeactive: (UIApplication *) Application { in } -  to- (void) Applicationwillterminate: (UIApplication *) Application { + } -  the @end

132 Sets the background color of the selected cell (Extended knowledge: Sets the background picture of the selected cell)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.