欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實(shí)例講解iOS應(yīng)用開發(fā)中使用UITableView創(chuàng)建自定義表格

 更新時(shí)間:2024年07月01日 11:56:38   作者:芳草小腳丫  
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用UITableView創(chuàng)建自定義表格的方法,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

一、帶索引目錄的表視圖

1.效果圖

201613093142428.png (320×478)

2.數(shù)據(jù)源

本想獲取通訊錄中得名字,但為了用模擬器調(diào)試方便,就寫死了數(shù)據(jù),所以也只寫了部分字母,總之有那么點(diǎn)意思就成

復(fù)制代碼 代碼如下:

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *sectionTitles; // 每個(gè)分區(qū)的標(biāo)題
    NSArray *contentsArray; // 每行的內(nèi)容
}

/** @brief 準(zhǔn)備數(shù)據(jù)源 在viewDidLoad方法中調(diào)用*/
- (void)readySource
{
    
    sectionTitles       = [[NSArray alloc] initWithObjects:
                           @"A",@"C",@"F",@"G",@"H",@"M",@"S",@"T",@"X",@"Z", nil];
    contentsArray       = [[NSArray alloc] initWithObjects:
                            @[@"阿偉",@"阿姨",@"阿三"],
                            @[@"蔡芯",@"成龍",@"陳鑫",@"陳丹",@"成名"],
                            @[@"芳仔",@"房祖名",@"方大同",@"芳芳",@"范偉"],
                            @[@"郭靖",@"郭美美",@"過兒",@"過山車"],
                            @[@"何仙姑",@"和珅",@"郝歌",@"好人"],
                            @[@"媽媽",@""],
                            @[@"孫",@"沈冰",@"嬸嬸"],
                            @[@"濤濤",@"淘寶",@"套娃"],
                            @[@"小二",@"夏紫",@"許",@"許"],
                            @[@"周",@"周",@"張",@"張"],nil];
}


3.顯示索引
復(fù)制代碼 代碼如下:

// 每個(gè)分區(qū)的頁眉
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [sectionTitles objectAtIndex:section];
}
// 索引目錄
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return sectionTitles;
}
        ④點(diǎn)擊索引,跳轉(zhuǎn)到點(diǎn)擊的分區(qū)

// 點(diǎn)擊目錄
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    // 獲取所點(diǎn)目錄對(duì)應(yīng)的indexPath值
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
    
    // 讓table滾動(dòng)到對(duì)應(yīng)的indexPath位置
    [tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    return index;
}


二、可以進(jìn)行行標(biāo)記的表視圖

1.效果圖

201613093224243.png (320×511)

2.在cellForRow方法中,將Cell的accessoryType設(shè)置為None

復(fù)制代碼 代碼如下:

// 定義其輔助樣式
       cell.accessoryType      = UITableViewCellAccessoryNone;

3.在didSelectRow方法中
復(fù)制代碼 代碼如下:

// 點(diǎn)擊行事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 獲取點(diǎn)擊行的cell
    UITableViewCell *cell   = [tableView cellForRowAtIndexPath:indexPath];
    
    // 如果cell已經(jīng)被標(biāo)記
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        // 取消標(biāo)記
        cell.accessoryType  = UITableViewCellAccessoryNone;
    }
    
    // 如果cell未標(biāo)記
    else{
        // 標(biāo)記cell
        cell.accessoryType  = UITableViewCellAccessoryCheckmark;
    }
    // 取消選中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

此時(shí),點(diǎn)擊行即可選中,取消選中,但是滾動(dòng)一下視圖吧,你會(huì)發(fā)現(xiàn)下面某些未被點(diǎn)擊的行也已經(jīng)被標(biāo)記了,這是因?yàn)閏ell的重用機(jī)制造成的,在第一篇文章中就這個(gè)問題有提到過

4.解決cell重用問題,在cellForRow方法中,定義cellIdetifier時(shí),將其每一行都定義為不同的值,就不會(huì)出現(xiàn)覆蓋,重復(fù)等現(xiàn)象了,但是這個(gè)方法太過粗暴,并不是最好的解決辦法,情急之下可以先用,然后再慢慢調(diào)試Table上的數(shù)據(jù)

復(fù)制代碼 代碼如下:

NSString *cellIdentifier = [NSString stringWithFormat:@"cellIdentifier%d%d",indexPath.row,indexPath.section];

三、定制表視圖的每一行內(nèi)容

1.我們做一個(gè)類似網(wǎng)易新聞客戶端的新聞列表的table,如下圖左;簡(jiǎn)易效果圖,如下圖右

201613093245537.png (320×511)201613093259537.png (320×469)

2.數(shù)據(jù)源,在interface中聲明

復(fù)制代碼 代碼如下:

NSMutableArray *news_MArray;// 新聞內(nèi)容數(shù)據(jù)源
    新建一個(gè)model類,命名為"newsModel",存放每一項(xiàng)數(shù)據(jù)

    newsModel.h如下,.m中沒有添加其他代碼,如果需要拷貝,可以重載copyWithZone方法,
#import <Foundation/Foundation.h>
 
typedef NS_ENUM(NSInteger, NEWSReportType){
    NEWSReportOrdinary, // 普通新聞
    NEWSReportExclusive,// 獨(dú)家新聞
    NEWSReportSpecial,  // 專題新聞
};
 
@interface newsModel : NSObject
 
@property (nonatomic, copy)NSString *       news_image;     //圖片
@property (nonatomic, copy)NSString *       news_title;     //標(biāo)題
@property (nonatomic, copy)NSString *       news_summary;   //摘要
@property (nonatomic, assign)NSInteger      news_replyNo;   //跟帖數(shù)量
@property (nonatomic, assign)NEWSReportType reportType;     //報(bào)道類型
 
 
@end


    在viewDidLoad方法中
復(fù)制代碼 代碼如下:

news_MArray = [[NSMutableArray alloc] init];
for(NSInteger index =0; index<10; index++){
    newsModel *model    = [[newsModel alloc] init];
    model.news_image    = [NSString stringWithFormat:@"%d.jpg",index+1];
    model.news_title    = @"曾在月光之下望煙花";
    model.news_summary  = @"曾共看夕陽漸降下 我怎么舍得去放下 要怎么舍得去放下";
    model.news_replyNo  = index+196;
    model.reportType    = index%3;
    
    [news_MArray addObject:model];
}

3.行數(shù)
復(fù)制代碼 代碼如下:

// 每個(gè)分區(qū)行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news_MArray count];
}

4.自定義cell上控件

    在cellForRow方法中if(cell==nil)前

復(fù)制代碼 代碼如下:

/*****自定義cell******/
newsModel *model    = [news_MArray objectAtIndex:indexPath.row];
 
UIImageView *   image_view;     //1.添加imageView
UILabel *       title_label;    //2.添加標(biāo)題Label
UILabel *       summary_label;  //3.添加摘要Label
UILabel *       replyNo_label;  //4.添加跟帖數(shù)量Label
UIButton  *     extra_view;     //5.屬于專題或者獨(dú)家報(bào)道,進(jìn)行標(biāo)記
/********************/

    在if(cell==nil)內(nèi)
復(fù)制代碼 代碼如下:

/*****自定義cell******/
        
        //1.添加imageView
        CGRect imageViewF   = CGRectMake(5, 5, 85, 65);
        image_view          = [[UIImageView alloc] initWithFrame:imageViewF];
        [cell addSubview:image_view];
        
        //2.添加標(biāo)題Label
        CGRect titleLabelF  = CGRectMake(95, 5, 230, 24);
        title_label         = [[UILabel alloc] initWithFrame:titleLabelF];
        title_label.font    = [UIFont systemFontOfSize:16];//字體大小
        [cell addSubview:title_label];
        
        //3.添加摘要Label
        CGRect summaryLabelF  = CGRectMake(97, 27, 210, 40);
        summary_label         = [[UILabel alloc] initWithFrame:summaryLabelF];
        summary_label.font    = [UIFont systemFontOfSize:12];    // 字體大小
        summary_label.textColor     = [UIColor darkGrayColor];  // 文字顏色
        summary_label.numberOfLines = 2;
        [cell addSubview:summary_label];
        
        //4.跟帖數(shù)量Label
        CGRect replyNoLabelF  = CGRectMake(210, 45, 95, 24);
        replyNo_label         = [[UILabel alloc] initWithFrame:replyNoLabelF];
        replyNo_label.font    = [UIFont systemFontOfSize:12];    // 字體大小
        replyNo_label.textColor     = [UIColor darkGrayColor];  // 文字顏色
        replyNo_label.textAlignment = NSTextAlignmentRight;      // 文字右對(duì)齊
        
        //5.專題extraView
        CGRect extraViewF       = CGRectMake(270, 50, 28, 14);
        extra_view              = [[UIButton alloc] initWithFrame:extraViewF];
        extra_view.titleLabel.font = [UIFont boldSystemFontOfSize:10];
        [extra_view setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        // 普通新聞,只添加跟帖數(shù)量
        if (model.reportType==NEWSReportOrdinary) {
           [cell addSubview:replyNo_label];
        }
        // 專題新聞,添加專題標(biāo)志,并添加跟帖數(shù)量
        else if(model.reportType == NEWSReportSpecial){
            
            // 設(shè)置背景色
            extra_view.backgroundColor = [UIColor colorWithRed:120.0/255.0 green:170.0/255.0 blue:245.0/255.0 alpha:1.0];
            
            [extra_view setTitle:@"獨(dú)家" forState:UIControlStateNormal];// 設(shè)置標(biāo)題
            
            [cell addSubview:extra_view];                               // 添加
            
            replyNo_label.frame = CGRectMake(170, 45, 95, 24);          // 改變跟帖數(shù)量Label的坐標(biāo)
            
            [cell addSubview:replyNo_label];                            // 添加跟帖數(shù)量Label
        }
        // 獨(dú)家新聞,只添加獨(dú)家標(biāo)志
        else if(model.reportType == NEWSReportExclusive){
            
            extra_view.backgroundColor = [UIColor redColor];            // 設(shè)置背景顏色
            
            [extra_view setTitle:@"專題" forState:UIControlStateNormal]; // 設(shè)置標(biāo)題
            
            [cell addSubview:extra_view];                               // 添加到cell
        }
/********************/

    在if(cell==nil)后
復(fù)制代碼 代碼如下:

/*****自定義cell******/
    [image_view setImage:[UIImage imageNamed:model.news_image]];// 設(shè)置圖片
    title_label.text    = model.news_title;                     // 設(shè)置標(biāo)題
    summary_label.text  = model.news_summary;                   // 設(shè)置小標(biāo)題
    replyNo_label.text  = [NSString stringWithFormat:@"%d 跟帖",model.news_replyNo];// 設(shè)置跟帖數(shù)量
/********************/

5.設(shè)置行高
復(fù)制代碼 代碼如下:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

相關(guān)文章

最新評(píng)論