UITableView (used by yourself) (attribute, pass value by proxy), uitableview

Source: Internet
Author: User

UITableView (used by yourself) (attribute, pass value by proxy), uitableview

Today, we are in a hurry.

As follows:

 

 

 

 

 

 

 

The Code is as follows:

#import <UIKit/UIKit.h>#import "FirstViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end

 

 

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    FirstViewController *first=[[FirstViewController alloc]init];    UINavigationController *navc=[[UINavigationController alloc]initWithRootViewController:first];    self.window.rootViewController=navc;    return YES;}

 

 

#import <UIKit/UIKit.h>#import "SecondViewController.h"@interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,postValueDelegate>@property(strong,nonatomic)UITableView * tableview;@property(strong,nonatomic)NSMutableArray *array;@property(strong,nonatomic)NSString *st;@end

 

 

 

# Import "FirstViewController. h "@ interface FirstViewController () @ property (assign, nonatomic) int a; @ end @ implementation FirstViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor yellowColor]; self. title = @ "Homepage"; UIBarButtonItem * nextItem = [[UIBarButtonItem alloc] initWithTitle: @ "next page" style: UIBarButtonItemStylePlain target: self action: @ selector (nextpage)]; self. navigationItem. rightBarButtonItem = nextItem; self. array = [NSMutableArray array]; for (int I = 1; I <20; I ++) {[self. array addObject: [NSString stringWithFormat: @ "you fell % d", I];} self. tableview = [[UITableView alloc] initWithFrame: self. view. frame style: 1]; self. tableview. separatorColor = [UIColor redColor]; self. tableview. delegate = self; self. tableview. dataSource = self; [self. view addSubview: self. tableview]; [self. tableview registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "cell"];}-(void) postvalue :( NSString *) str {// self. st = str; [self. array replaceObjectAtIndex: self. a withObject: str]; [self. tableview reloadData]; // NSLog (@ "% @", self. st) ;}# set the number of lines displayed in each partition of The pragma mark Data Source-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. array. count ;}# content displayed in each cell of The pragma mark Data Source-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// cell reuse mechanism static NSString * cellIdentity = @ "cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellIdentity forIndexPath: indexPath]; cell. textLabel. text = self. array [indexPath. row]; return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "% @", self. array [indexPath. row]); self. st = self. array [indexPath. row]; SecondViewController * second = [[SecondViewController alloc] init]; second. str = self. st; second. delegate = self; self. a = (int) indexPath. row; NSLog (@ "% d", self. a); [self. navigationController pushViewController: second animated: YES];}-(void) nextpage {SecondViewController * second = [[SecondViewController alloc] init]; second. str = self. st; [self. navigationController pushViewController: second animated: YES];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end

 

# Import <UIKit/UIKit. h> // create a protocol. The declaration method @ protocol postValueDelegate <NSObject>-(void) postvalue :( NSString *) str; @ end @ interface SecondViewController: UIViewController <UITextFieldDelegate> @ property (strong, nonatomic) UITextField * textName; @ property (strong, nonatomic) NSString * str; @ property (strong, nonatomic) id <postValueDelegate> delegate; @ end

 

 

# Import "SecondViewController. h "@ interface SecondViewController () @ end @ implementation SecondViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor greenColor]; self. navigationItem. hidesBackButton = YES; self. title = @ "last page"; self. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "back" style: 2 target: self action: @ selector (backpage)]; self. textName = [[UITextField alloc] initWithFrame: CGRectMake (100,300,150, 60)]; self. textName. borderStyle = 1; self. textName. text = self. str; self. textName. delegate = self; [self. view addSubview: self. textName];}-(BOOL) textFieldShouldReturn :( UITextField *) textField {if ([textField isFirstResponder]) {[textField resignFirstResponder];} if (self. delegate) {[self. delegate postvalue: self. textName. text];} [self. navigationController popToRootViewControllerAnimated: YES]; return YES;}-(void) backpage {if (self. delegate) {[self. delegate postvalue: self. textName. text]; NSLog (@ "% @", self. textName. text);} [self. navigationController popToRootViewControllerAnimated: YES];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end

 

Related Article

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.