zoukankan      html  css  js  c++  java
  • FirstApp,iphone开发学习总结2,简单的表

    首先在TableViewController.h中,将UIViewController改成UITableViewController(委托先不写),并创建一个数组:

    @interface TableViewController : UITableViewController{
        NSMutableArray *data;
    }

    在init方法中,我们初始化此表为分组表:

    - (id)init {
        self = [super initWithStyle:UITableViewStyleGrouped];//分组样式
        if (self) {
            [self setTitle:@"表格展示"];
            UIImage *img = [UIImage imageNamed:@""];
            [[self tabBarItem] setImage:img];
        }
        return self;
    }
    - (id)initWithStyle:(UITableViewStyle)style
    {
        return [self init];
    }

    在- (void)viewDidLoad中初始并实例化数组:

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

    设置分组数://不是分组表,可以忽略,默认为1.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [data count];
    }

    设置每组的行数:*必须实现//理解section的意思

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == 0) {
            return 1;
        }else if(section == 1){
            return 2;
        }else{
            return 3;
        }
    }

    设置每行的数据:*必须实现//理解标识符(Identifier)的意思

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"FirstAppTableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        cell.textLabel.text = [data objectAtIndex:[indexPath row]];
        return cell;
    }

    最后在- (void)dealloc释放data:

    [data release];
  • 相关阅读:
    Linux nfs服务讲解
    Linux nfs服务介绍
    牛客网题目-数组中只出现1次的数字
    牛客网中矩阵中的路径
    求链表的第一个公共节点
    C++中STL中简单的Vector的实现
    牛客网栈的压入,和弹出序列
    C++智能指针
    CI Weekly #22 | flow.ci 新版 iOS 构建流程的 4 大变化
    CI Weekly #21 | iOS 持续集成快速入门指南
  • 原文地址:https://www.cnblogs.com/maxfong/p/2481924.html
Copyright © 2011-2022 走看看