Simple usage of UITableView

Source: Internet
Author: User

#import "AppDelegate.h"

@interface Appdelegate ()

@end

@implementation Appdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Initializes an object of a Viewcontroller

Viewcontroller *view = [[Viewcontroller alloc] init];

Add the navigation bar to the top

Uinavigationcontroller *NAVC = [[Uinavigationcontroller alloc] initwithrootviewcontroller:view];

Self.window.rootViewController = NAVC;

Initialize a second page

Secondviewcontroller *SECONDVC = [[Secondviewcontroller alloc] init];

//

SecondVc.tabBarItem.title = @ "Second";

SecondVc.tabBarItem.badgeValue = @ "2";

Uitabbarcontroller *tabbarcon = [[Uitabbarcontroller alloc] init];

Tabbarcon.tabBar.tintColor = [Uicolor Greencolor];

[Tabbarcon SETVIEWCONTROLLERS:@[NAVC,SECONDVC]];

Self.window.rootViewController =tabbarcon;

return YES;

}

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface viewcontroller:uiviewcontroller<uitableviewdatasource,uitableviewdelegate>

@property (nonatomic, strong) UITableView *tablev;

@property (nonatomic, strong) Nsmutablearray *arry;

@end

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Set the biaot of the navigation bar

Self.title = @ "First";

Set a button that triggers a jump to the next page

Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] initwithimage:[uiimage imagenamed:@ "Arrow_normal" ] Style:uibarbuttonitemstyleplain target:self Action: @selector (Tableview:didselectrowatindexpath:)];

Initializing a variable group

Self.arry = [Nsmutablearray array];

Loop to assign values to an array

for (int i = 0; i<; i++) {

[Self.arry addobject:[nsstring stringwithformat:@ "%i%i", I,i]];

}

Initialize the size and style of the Tablev

Self.tablev = [[UITableView alloc] InitWithFrame:self.view.frame Style:uitableviewstyleplain];

Specifies that the Self.tableV.delegate proxy is self

Self.tableV.delegate = self;

Set the color for the separator bar

Self.tableV.separatorColor = [Uicolor Bluecolor];

Specifies that the Self.tableV.dataSource proxy is self

Self.tableV.dataSource = self;

Load the Tablev on the view

[Self.view AddSubview:self.tableV];

Specifies the unique identity of the cell

[Self.tablev Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "cell"];

}

Define a global variable to receive the index Indexpath.row

int num;

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

Set the number of rows to display per partition

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{

return self.arry.count;

}

What each cell displays

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

static NSString *cellidentity = @ "Cell";

Cell reuse mechanism

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentity Forindexpath:indexpath];

Cell.textLabel.text = Self.arry[indexpath.row];

Cell.textLabel.textAlignment = Nstextalignmentcenter;

Add a background color to the cell

Cell.backgroundcolor = [Uicolor Yellowcolor];

return cell;

}

Proxy method, which displays information for the selected cell

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{

Initializes an Secondviewcontroller object

Secondviewcontroller *second = [[Secondviewcontroller alloc] init];

Assign the value of Self.arry[indexpath.row] to Second.str

Second.str = Self.arry[indexpath.row];

Receive the appropriate index with NUM

num = (int) Indexpath.row;

Defines the value of a string str receives Self.arry[indexpath.row]

NSString *str =self.arry[indexpath.row];

Initialize a cue bar Uialertcontroller object and assign him the value

Uialertcontroller *alertc= [Uialertcontroller alertcontrollerwithtitle:@ "You are sure to click on it" Message:str PreferredStyle: Uialertcontrollerstylealert];

Define two uialertaction, *action2 cancel

Uialertaction *action2 = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstyledefault Handler:nil];

*action1 OK

Uialertaction *action1 = [uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull action) {

Click OK to go to the next page

[Self.navigationcontroller Pushviewcontroller:second Animated:yes];

}];

Add the Action1,action2 to the ALERTC

[ALERTC Addaction:action1];

[ALERTC Addaction:action2];

Show ALERTC

[Self PRESENTVIEWCONTROLLER:ALERTC animated:yes completion:nil];

}

Add a listener before the form is loaded

-(void) Viewwillappear: (BOOL) animated{

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (mypostvalue:) name:@ "Lamco" object:nil];

}

Add Observer

-(void) Mypostvalue: (nsnotification *) noti{

[Self.arry Replaceobjectatindex:num WithObject:noti.object];

[Self.tablev Reloaddata];

}

Removing observers

-(void) dealloc{

[[Nsnotificationcenter Defaultcenter] removeobserver:self name:@ "Lamco" object:nil];

NSLog (@ "destroy notice, remove observer");

}

@end

#import <UIKit/UIKit.h>

@protocol postvaluedelegate <NSObject>

-(void) Postvalue: (NSString *) Value;

@end

@interface secondviewcontroller:uiviewcontroller<uitextfielddelegate>

@property (nonatomic, strong) NSString *str;

@property (nonatomic, strong) Uitextfield *textfld;

@property (nonatomic, assign) id<postvaluedelegate> delegate;

@end

#import "SecondViewController.h"

@interface Secondviewcontroller ()

@end

@implementation Secondviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Add a background picture

Uiimageview *imagev= [[Uiimageview alloc] initWithFrame:self.view.frame];

Imagev.image = [UIImage imagenamed:@ "82r58picyem_1024.jpg"];

Self.view.backgroundColor = [Uicolor colorWithPatternImage:imageV.image];

Initial text Box textfld

SELF.TEXTFLD = [[Uitextfield alloc] Initwithframe:cgrectmake (100, 200, 200, 100)];

Set a background picture for a text box

Uiimageview *IMAGEV1 = [[Uiimageview alloc] initWithFrame:self.textfld.frame];

Imagev1.image = [UIImage imagenamed:@ "27u58picvxi_1024.jpg"];

Self.textfld.backgroundColor = [Uicolor colorWithPatternImage:imageV1.image];

Specify Proxy

Self.textfld.delegate = self;

Passing value to text box by property str

Self.textfld.text = Self.str;

Center the text

Self.textfld.textAlignment = Nstextalignmentcenter;

Show Textfld on view

[Self.view ADDSUBVIEW:SELF.TEXTFLD];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

Hide keyboard

-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{

1. Notice

Nsnotification *noti = [[Nsnotification alloc] initwithname:@ "Lamco" Object:textField.text Userinfo:nil];

Uniseriate

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

if ([TextField Isfirstresponder]) {

[TextField Resignfirstresponder];

}

[Self.navigationcontroller Poptorootviewcontrolleranimated:yes];

return YES;

}

@end

Simple usage of UITableView

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.