UI總結(jié)-tableView的界面?zhèn)髦?/h1>

? ? ? ?UI總結(jié)-tableView的界面?zhèn)髦?br>

因為tableView在以后的開發(fā)占了很重要的地位,所以把tableView的界面?zhèn)髦祮为毮贸鰜碜隽诉@一篇,里面涉及了tableView界面之間的屬性,協(xié)議傳值和tableView的刷新等功能.

ViewController.m文件:


#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()

@property(nonatomic,retain)NSArray *arr1;

@property(nonatomic, retain)NSMutableArray *arr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

//定義數(shù)組,里面存數(shù)據(jù)

self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"盧俊義", @"吳用", @"公孫勝", @"關(guān)勝", @"林沖", @"秦明" ,@"呼延灼" , @"花榮",@"柴進", @"李應(yīng)", @"朱仝",@"魯智深",@"武松", @"徐寧", @"張清", @"楊志", @"董平", @"索超", @"戴宗", @"劉唐", @"李逵", @"史進", @"穆弘", @"雷橫", @"李俊",nil];

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

[self.view addSubview:tableView];

[tableView release];

tableView.dataSource = self;

tableView.delegate = self;

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click:)]autorelease];

tableView.tag = 1000;

[self creatData];

}

-(void)creatData{

//在本地找名為stu.plist的文件

NSString *path = [[NSBundle mainBundle]pathForResource:@"stu" ofType:@"plist"];

NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

//將本地的數(shù)據(jù)顯示在頁面上

self.title = [dic valueForKey:@"name"];

}

//rightBarButtonItem的點擊方法

-(void)click:(UIBarButtonItem *)button{

SecondViewController *vc = [[SecondViewController alloc]init];

[self.navigationController pushViewController:vc animated:YES];

[vc release];

}

//實現(xiàn)協(xié)議方法

-(void)sendValue:(NSString *)str{

//把傳過來的人名加到屬性數(shù)組里

[self.arr addObject:str];

UITableView *table = (UITableView *)[self.view viewWithTag:1000];

//刷新tableView

[table reloadData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *reuse = @"reuse";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

if (!cell) {

cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];

}

cell.textLabel.text = self.arr [indexPath.row];

return cell;

}

//tableView的點擊方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"%@",self.arr[indexPath.row]);

SecondViewController *vc = [[SecondViewController alloc]init];

[self.navigationController pushViewController:vc animated:YES];

vc.str = self.arr[indexPath.row];

[vc release];

//簽協(xié)議

vc.delegate = self;

}

secondController.h文件:

@protocol SecondViewControllerDelegate

- (void)sendValue:(NSString *)str;

@end

@interface SecondViewController : UIViewController@property(nonatomic, copy)NSString *str;

@property(nonatomic,assign)iddelegate;

@end

secondController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = self.str;

self.view.backgroundColor = [UIColor grayColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(100, 100, 150, 150);

[button setTitle:@"返回" forState:UIControlStateNormal];

[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

button.backgroundColor = [UIColor redColor];

[self.view addSubview:button];

button.layer.borderWidth = 1;

button.layer.cornerRadius = 5;

}

-(void)back:(UIButton *)button{

[self.navigationController popToRootViewControllerAnimated:YES];

//協(xié)議傳值

[self.delegate sendValue:@"水滸傳"];

}

運行的效果:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者

  • 序言:七十年代末怎抛,一起剝皮案震驚了整個濱河市卑吭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌马绝,老刑警劉巖豆赏,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異富稻,居然都是意外死亡河绽,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門唉窃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耙饰,“玉大人,你說我怎么就攤上這事纹份」豆颍” “怎么了?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵蔓涧,是天一觀的道長件已。 經(jīng)常有香客問我,道長元暴,這世上最難降的妖魔是什么篷扩? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮茉盏,結(jié)果婚禮上鉴未,老公的妹妹穿的比我還像新娘。我一直安慰自己鸠姨,他們只是感情好铜秆,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著讶迁,像睡著了一般连茧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天啸驯,我揣著相機與錄音客扎,去河邊找鬼。 笑死罚斗,一個胖子當(dāng)著我的面吹牛虐唠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播惰聂,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼疆偿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搓幌?” 一聲冷哼從身側(cè)響起杆故,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎溉愁,沒想到半個月后处铛,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡拐揭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年撤蟆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片堂污。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡家肯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出盟猖,到底是詐尸還是另有隱情讨衣,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布式镐,位于F島的核電站反镇,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏娘汞。R本人自食惡果不足惜歹茶,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望你弦。 院中可真熱鬧惊豺,春花似錦、人聲如沸鳖目。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽领迈。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狸捅,已是汗流浹背衷蜓。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留尘喝,地道東北人磁浇。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像朽褪,于是被迫代替她去往敵國和親置吓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容