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];
  • 相关阅读:
    .NET Interop 工具集
    关于正弦波的算法
    Windows Phone 系列 本地数据存储
    Xaml cannot create an instance of “X”
    Windows Phone 系列 使用 MVVM绑定时无法获取当前值
    Windows Phone 系列 应用程序图标无法显示
    Windows Phone 系列 WPConnect无法上网的问题
    Windows Phone 系列 使用 Windows Phone 保存铃声任务
    WP7.5提交应用
    Windows Phone 系列 动态删除ObservableCollection
  • 原文地址:https://www.cnblogs.com/maxfong/p/2481924.html
Copyright © 2011-2022 走看看