iPhone開(kāi)發(fā) UITableView代碼實(shí)現(xiàn)實(shí)例
iPhone開(kāi)發(fā) UITableView代碼實(shí)現(xiàn)實(shí)例是本文要介紹的內(nèi)容,介紹了UITableView的基本操作,以小實(shí)例實(shí)現(xiàn),我們來(lái)看內(nèi)容。
1、前題條件
1.1 UITableView不能直接加載到窗體中,窗體中首先要加載一個(gè)UIView控件,然后才可以添加UITableView
1.2 UITableView需要繼續(xù)兩個(gè)協(xié)議<UITableViewDelegate,UITableViewDataSource>
兩個(gè)協(xié)議的方法為:
--行的數(shù)量:
- -(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
- {
- return [tableArray count];
- }
- --行的定義
- -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- SimpleTableIdentifier];
- if (cell == nil)
- {
- //默認(rèn)樣式
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier: SimpleTableIdentifier] autorelease];
- }
- //文字的設(shè)置
- NSUInteger row=[indexPath row];
- cell.textLabel.text=[tableArray objectAtIndex:row];
- return cell;
- }
2、添加UIView
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320,260)];
- //自定識(shí)別頭及高度
- view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;
- view.backgroundColor = [UIColor blueColor];
- //將view添加到主窗體
- [self.view addSubview:view];
3、添加UITableView
- UITableView *table1=[[UITableView alloc] initWithFrame:CGRectMake(0, 0.00, 300, 250)];
- table1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;
- table1.backgroundColor=[UIColor redColor];
- //添加到view窗體上
- [view addSubview:table1];
- table1.delegate=self;
- table1.dataSource=self;
4、取消點(diǎn)擊動(dòng)畫
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
5、滾動(dòng)路徑標(biāo)識(shí),直至指數(shù)連續(xù)接收器在屏幕上的特定位置
- - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:
- (BOOL)animated Parameters
indexPath
索引路徑標(biāo)識(shí)在其行的索引,其部分指標(biāo)表視圖行。
scrollPosition
一個(gè)常數(shù),在接收標(biāo)識(shí)表查看行(上,中,下)的相對(duì)位置滾動(dòng)時(shí)結(jié)束。
animated
是如果你要?jiǎng)赢嬙谖恢酶淖?,沒(méi)有是否應(yīng)立即生效。
實(shí)例代碼:
- float height = 216.0;
- CGRect frame = m_pTableView.frame;
- frame.size = CGSizeMake(frame.size.width, frame.size.height - height);
- [UIView beginAnimations:@"Curl"context:nil];//動(dòng)畫開(kāi)始
- [UIView setAnimationDuration:0.30];
- [UIView setAnimationDelegate:self];
- [m_pTableView setFrame:frame];
- [m_pTableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
- [UIView commitAnimations];
6、獲取選中的列,如圖所示:
代碼如下:
- for (NSInteger i = 0; i < [[peopleTableView visibleCells] count]; i++)
- {
- UITableViewCell *cell = [[peopleTableView visibleCells] objectAtIndex:i];
- if (cell.accessoryType == UITableViewCellAccessoryCheckmark) //將勾選的人員添加到列表中
- {
- Item *item = (Item *)[NextPeopleList.SelectList objectAtIndex:i];
- [peopleList addObject:item];
- isCheck = TRUE;
- }
- }
7、Cell將要顯示時(shí)事件
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (indexPath.row % 2 == 0) {
- cell.backgroundColor = DarkColor;
- }else {
- cell.backgroundColor = LightColor;
- }
小結(jié):iPhone開(kāi)發(fā) UITableView代碼實(shí)現(xiàn)實(shí)例的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!