iOS tableview 小总结 (1)

  我发现IOS里的tableview真是很厉害,几乎都能用到它,我今天就总结一下,这几天所学到的知识,如有指正,感激不尽

1.首先我们得在m文件中创好要建立的表,初始化先定义好了,

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);"><pre name="code" class="objc"><pre name="code" class="objc">@interface SelfViewController ()
{
    UITableView *tableview;
}
- (void)setvies;
@end

 

 2.我们要初始化数据,其实有这个viewDidLoad函数,但是我们最好另写一个(师傅就是这样告诉我的)
<pre name="code" class="objc">- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibBundleOrNil bundle:nibBundleOrNil];
    if(self) {
        [self setvies];
    }
    return self;
}
 在这里写也可以 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setvies];
}
3.我们这时就要画表了,就是定一下它的基本特性
//表的方法
- (void) setvise {
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-80-64) style:UITableViewStyleGrouped];
    //[tableview setDelegate:self];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.showsVerticalScrollIndicator = NO;
    tableview.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [self.view addSubview:tableview];
    
    
}
//section 间距
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}
-(CGFloat) tableView:(UITableView *)tableView HeightForHeaderInSection:(NSInteger)section
{
    return 2;
}
// 表中的结构
- (NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else if(section == 1)
        return 1;
    return 3;
}
// 有几个section
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
这里两句比较怪
tableview. delegate = self;

tableview.dataSource =self;

这是委托,我的理解就是和数据建立联系,这个委托,等我学明白再来记录吧

4.我们要添加数据了

//插入数据
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    switch (indexPath.section) {
        case 1:
            //if (indexPath.row == 0)
            {
                UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 13.5, 16, 16)];
                imgview.image = [UIImage imageNamed:@"gr_zjbb@2x.png"];
                [cell addSubview:imgview];
                
                cell.textLabel.text = @"            我的哔哔";
                cell.textLabel.textColor = fontHui;
                cell.backgroundColor = Hui;
                detaiLab = [[UILabel alloc]initWithFrame:CGRectMake(265, 13.5, 290, 18 )];
                detaiLab.textColor = fontHui;
                [cell addSubview:detaiLab];
                
            }
            break;
            
        case 2:
        {
            cell.textLabel.textColor = fontHui;
            cell.backgroundColor = Hui;
            if (indexPath.row == 0) {
                UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 13.5, 16, 16)];
                imgview.image = [UIImage imageNamed:@"gr_xzjl@2x.png"];
                [cell addSubview:imgview];
                
                cell.textLabel.text = @"            下载记录";
                //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                
                cell.textLabel.textColor = fontHui;
                cell.backgroundColor = Hui;
                
            }
            else if(indexPath.row == 1) {
                UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 13.5, 16, 16)];
                imgview.image = [UIImage imageNamed:@"gr_bfjl@2x.png"];
                [cell addSubview:imgview];
                
                cell.textLabel.text = @"            播放记录";
                cell.textLabel.textColor = fontHui;
                cell.backgroundColor = Hui;
                
            }
            else {
                UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 13.5, 16, 16)];
                imgview.image = [UIImage imageNamed:@"gr_sz@2x.png"];
                [cell addSubview:imgview];
                
                cell.textLabel.text = @"            设置";
                cell.textLabel.textColor = fontHui;
                cell.backgroundColor = Hui;
            }
            
        }
            break;
        default:
            break;
    }
    return cell;
    
}

这里加的都是死的数据

5.添加点击方法

//触发事件
- (void) tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //快速回复按下去的cell
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //点击我的哔哔
  if(indexPath.row == 0 && indexPath.section == 1){
        MybibiViewController *mybibi = [[MybibiViewController alloc]init];
        [self.navigationController pushViewController:mybibi animated:YES];
        mybibi.title = @"我的哔哔";
    }
    //下载记录
    if(indexPath.row == 0 && indexPath.section == 2){
        DownloadViewController *down = [[DownloadViewController alloc]init];
        [self.navigationController pushViewController:down animated:YES];
    }
    
    //播放记录收听历史
    if(indexPath.row == 1 && indexPath.section == 2){
        HistoryViewController *history = [[HistoryViewController alloc]init];
        [self.navigationController pushViewController:history animated:YES];
        history.title = @"收听历史";
    }
    //设置
    if(indexPath.row == 2 && indexPath.section == 2){
        SetViewController *Set = [[SetViewController alloc]init];
        [self.navigationController pushViewController:Set animated:YES];
        Set.title = @"设置";
    }
}
就是这些了。



posted @ 2014-10-29 09:32  赫凯  阅读(26)  评论(0)    收藏  举报