UITableView詳解

1、創建 UITableView數組

 

DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];

 [DataTable setDelegate:self];

 [DataTable setDataSource:self];

 [self.view addSubview:DataTable];

 

 

2、UITableView各Method說明promise

//Section總數spa

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

 return TitleData;

}

 

 

// Section Titlescode

//每一個section顯示的標題blog

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

 return @"";

}

 

 

//指定有多少個分區(Section),默認爲1索引

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 return 4;

}

 

 

//指定每一個分區中有多少行,默認爲1事件

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

}

 

 

//繪製Cell圖片

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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

  

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:

                             SimpleTableIdentifier];

    if (cell == nil) {  

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

                                       reuseIdentifier: SimpleTableIdentifier] autorelease];

 }

 cell.imageView.image=image;//未選cell時的圖片

 cell.imageView.highlightedImage=highlightImage;//選中cell後的圖片

 cell.text=//.....

 return cell;

}

 

 

//行縮進ip

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

 NSUInteger row = [indexPath row];

 return row;

}

 

 

//改變行的高度rem

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 40;

}

 

 

//定位

[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];

 

 

//返回當前所選cell

NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];

[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];

 

[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];

 

 

//選中Cell響應事件

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

 [tableView deselectRowAtIndexPath:indexPath animated:YES];//選中後的反顯顏色即刻消失

}

 

 

//判斷選中的行(阻止選中第一行)

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row = [indexPath row];

    if (row == 0)

        return nil;

   

    return indexPath;

}

 

 

//划動cell是否出現del按鈕

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

}

 

 

//編輯狀態

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

}

[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];

 

//右側添加一個索引表

 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

}

 

 

 

//返回Section標題內容

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

}

 

//自定義划動時del按鈕內容

- (NSString *)tableView:(UITableView *)tableView

titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

 

//跳到指的row or section

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

 

3、在UITableViewCell上創建UILable多行顯示

 

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

    static NSString *CellIdentifier = @"Cell";   

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

  UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];

  [Datalabel setTag:100];

  Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

  [cell.contentView addSubview:Datalabel];

  [Datalabel release];

 } 

 UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];

 [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];

 Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

 

 

 

//選中cell時的顏色

typedef enum {

    UITableViewCellSelectionStyleNone,

    UITableViewCellSelectionStyleBlue,

    UITableViewCellSelectionStyleGray

} UITableViewCellSelectionStyle 

 

//cell右邊按鈕格式

typedef enum {

    UITableViewCellAccessoryNone,                   // don't show any accessory view

    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track

    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks

    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track

} UITableViewCellAccessoryType

 

//是否加換行線

typedef enum {

    UITableViewCellSeparatorStyleNone,

    UITableViewCellSeparatorStyleSingleLine

} UITableViewCellSeparatorStyle//改變換行線顏色

tableView.separatorColor = [UIColor blueColor];

 

3、設置cell邊框線長度

 tableView.separatorInset=UIEdgeInsetsZero;

 

 

4、設置cell邊框線顏色

 

 tableView.separatorColor = Colour(224, 231, 227, 1);

 

 

 

   5、設置沒有內容的cell沒有分割線

    原理:重新對footView進行重置

    table.tableFooterView = [[UIView alloc] init];

 

 

6、設置UITableView是否顯示滾動條

 

[table setShowsHorizontalScrollIndeiator:YES];  //設置水平滾動條可見

[table setShowsVerticalScrollIndicator:YES];      //設置垂直滾動條可見

 

7、當點擊cell時顯示灰色,取消點擊後顯示正常顏色

 [tableView deselectRowAtIndexPath:indexPath animated:YES];

 

 

8、刪除某一行

//設置刪除時編輯狀態

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle==UITableViewCellEditingStyleDelete) {  //若是是刪除就從數組中移除元素

        [list removeObjectAtIndex:indexPath.row];

        [contentTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

}

 

////修更名字

//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    return @"下載";

 

//}

 

9、添加一行

1、———-----

 [hist getHistoryModel:^(HistoryCaseModel *model) {

        [self.historyCaseArray addObject:model];

       [contentTableView beginUpdates];

       [contentTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.historyCaseArray.count-1 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];

       [contentTableView endUpdates];

//

   }];


 

2、----------

- (void)viewDidLoad

{

    [super viewDidLoad];

    contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

    [contentTableView setEditing:YES animated:YES];

    contentTableView.delegate = self;

    contentTableView.dataSource =self;

    

    [self.view addSubview:contentTableView];

    

    

    array = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];

}

#pragma mark - UITableViewDelegate**UITableViewDataSource

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

{

    return array.count;

}

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

{

    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    }

    cell.textLabel.text = array[indexPath.row];

    

    return cell;

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row = [indexPath row];

    if(editingStyle==UITableViewCellEditingStyleInsert)

    {

        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath, nil];

        [array insertObject:@"insert new cell" atIndex:row];

        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];

    }

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return UITableViewCellEditingStyleInsert;

}

 

//設置分割線長度

[self.contentTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];

 

//加載xib文件cell

   cell=[[[NSBundle mainBundle]loadNibNamed:@"ActivitieTableViewCell" owner:self options:nil]lastObject];
相關文章
相關標籤/搜索