Basic knowledge of IOS development-fragment 15, basic knowledge of ios-Fragment
1. Convert custom objects into NsData and store them in the database.
To convert to an nsdata custom object, follow the <NSCoding> protocol, and then encodeWithCoder and initwithcode are implemented to convert the property. The example is HMShop. h # import <Foundation/Foundation. h> @ interface HMShop: NSObject <NSCoding> @ property (nonatomic, copy) NSString * name; @ property (nonatomic, assign) double price; @ endHMShop. m # import "HMShop. h "@ implementation HMShop-(void) encodeWithCoder :( NSCoder *) encoder {[encoder encodeObject: self. name forKey: @ "name"]; [encoder encodeDouble: self. price forKey: @ "price"];}-(id) initWithCoder :( NSCoder *) decoder {if (self = [super init]) {self. name = [decoder decodeObjectForKey: @ "name"]; self. price = [decoder decodeDoubleForKey: @ "price"];} return self;}-(NSString *) description {return [NSString stringWithFormat: @ "% @ <-> % f ", self. name, self. price] ;}@ end operation:-(void) addShops {NSMutableArray * shops = [NSMutableArray array]; for (int I = 0; I <100; I ++) {HMShop * shop = [[HMShop alloc] init]; shop. name = [NSString stringWithFormat: @ "item -- % d", I]; shop. price = arc4random () % 10000; NSData * data = [NSKeyedArchiver archivedDataWithRootObject: shop]; [self. db executeUpdateWithFormat: @ "insert into t_shop (shop) VALUES (% @);", data] ;}}-(void) readShops {FMResultSet * set = [self. db executeQuery: @ "SELECT * FROM t_shop LIMIT 10, 10;"]; while (set. next) {NSData * data = [set objectForColumnName: @ "shop"]; HMShop * shop = [NSKeyedUnarchiver unarchiveObjectWithData: data]; NSLog (@ "% @", shop);} * reasons for converting an object to nsdata, because it will become a string when it is stored in the database, which is unfavorable for conversion. Therefore, serialize the object to nsdata and then store it in the database, it is also nsdata before conversion;
2: added a sub-controller to extract some public content la S and slim down the current viewcontroller.
DetailsViewController *details = [[DetailsViewController alloc] init]; details.photo = self.photo; details.delegate = self; [self addChildViewController:details]; CGRect frame = self.view.bounds; frame.origin.y = 110; details.view.frame = frame; [self.view addSubview:details.view]; [details didMoveToParentViewController:self];
3: Use the Protocol to separate calls
Create a protocol in the sub-controller and process it internally to transmit parameters to the sub-controller. h @ protocol DetailsViewControllerDelegate-(void) Authorization :( NSString *) key; @ end @ interface DetailsViewController: UITableViewController @ property (nonatomic, strong) Photo * photo; @ property (nonatomic, weak) id <DetailsViewControllerDelegate> delegate; @ end subcontroller. m-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSString * key = self. keys [(NSUInteger) indexPath. row]; // transmits parameters to implement [self. delegate didSelectPhotoAttributeWithKey: key];} parent controller. m @ interface PhotoViewController () <DetailsViewControllerDelegate> @ end (obtain the parameter and perform the operation on the original subcontroller):-(void) didSelectPhotoAttributeWithKey :( NSString *) key {DetailViewController * detailViewController = [[DetailViewController alloc] init]; detailViewController. key = key; [self. navigationController pushViewController: detailViewController animated: YES];}
4: Use of kvo
// When the progress value changes, the kvo value is added. The key is fractionCompleted-(void) setProgress :( NSProgress *) progress {if (_ progress) {[_ progress removeObserver: self forKeyPath: @ "fractionCompleted"];} _ progress = progress; if (_ progress) {[_ progress addObserver: self forKeyPath: @ "fractionCompleted" options: NSKeyValueObservingOptionNew context: nil];} // message kvo message-(void) dealloc {if (_ progress) {[_ progress removeObserver: self forKeyPath: @ "fractionCompleted"];} _ progress = nil ;} # pragma mark KVO-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {if ([keyPath is1_tostring: @ "fractionCompleted"]) {NSProgress * progress = (NSProgress *) object; NSProgress * cellProgress = _ offsourecebean. cDownloadTask. progress; BOOL belongSelf = NO; if (cellProgress & cellProgress = progress) {belongSelf = YES;} dispatch_async (dispatch_get_main_queue (), ^ {if (self) {[self showProgress: progress belongSelf: belongSelf] ;}}) ;}else {[super observeValueForKeyPath: keyPath ofObject: object change: change context: context] ;}} * After adding a listener, you need to delete it when you are not using it. Here, addObserver can be another object, and then implement the observeValueForKeyPath protocol internally. You can set the options type when adding a listener, multiple types can also be used together; for example, NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld; when the Monitored object changes, it will immediately notify the Monitored object so that it can make some responses, such as view update;
5: Customize the accessoryView of UITableViewCell to determine which Button is pressed
During UITableview development, you often need to customize the AccessoryView on the right of the Cell, replace it with a button with an image, and determine which Custom button is pressed when you Tap it. Create Custom button and set it to AccessoryViewif (cell = nil) {cell = [[UITableView alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier]; UIImage * image = [UIImage imageNamed: @ "delete.png"]; UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; CGRect frame = CGRectMake (0.0, 0.0, image. size. width, image. size. height); button. frame = frame; [button setBackgroundImage: image State: UIControlStateNormal]; button. backgroundColor = [UIColor clearColor]; [button addTarget: self action: @ selector (buttonPressedAction forControlEvents: UIControlEventTouchUpInside]; cell. accessoryView = button;} if you add the Button to the cell. contentView. Cell. contentView addSubview: button]; make a judgment when you Tap the Cell to obtain the IndexPath-(void) buttonPressedAction id of the user Tap) sender {UIButton * button = (UIButton *) sender; (UITableViewCell *) cell = [button superview]; int row = [myTable indexPathForCell: cell]. row;} for the Button (UITableViewCell *) added to contentview cell = [[button superview];
6: directly use the UITableViewCell provided by the system. The cell. accessoryView can be used to customize the control.
# Import "MyselfViewController. h "@ interface MyselfViewController () @ property (nonatomic, retain) NSMutableArray * datasource; @ end @ implementation MyselfViewController-(void) dealloc {[_ datasource release]; [super dealloc];}-(NSMutableArray *) datasource {if (! _ Datasource) {self. datasource = [NSMutableArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "MyselfList" ofType: @ "plist"];} return _ datasource;}-(instancetype) init {self = [super initWithStyle: UITableViewStyleGrouped]; if (self) {} return self ;}- (void) viewDidLoad {[super viewDidLoad]; [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "cell"]; self. tableView. rowHeight = 70; self. navigationItem. title = @ "my";}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-Table view data source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return self. datasource. count;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// Return the number of rows in the section. return [self. datasource [section] count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView progress: @ "cell" forIndexPath: indexPath]; NSDictionary * dict = [self. datasource [indexPath. section] objectAtIndex: indexPath. row]; cell. textLabel. text = dict [@ "title"]; cell. imageView. image = [UIImage imageNamed: dict [@ "imageName"]; if (indexPath. section = 2 & indexPath. row = 0) {cell. accessoryView = [[[UISwitch alloc] init] autorelease];} else {cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;} return cell;} @ end