【问题标题】:How to display the UITableView programmatically?如何以编程方式显示 UITableView?
【发布时间】:2010-07-26 09:18:21
【问题描述】:

我想问一个关于目标 C 的 UITableView 的问题。我正在编写一个程序,我想以编程方式创建 UI。但是,我不知道如何以编程方式显示表格。我已经有一个 NSMutableArray 来存储显示的数据。我创建了一个对象UITableView *tableData;,下一步我该怎么做?非常感谢。

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    这样的事情应该可以解决问题(假设这是在您的视图控制器中运行并且您为表格视图设置了一个属性):

    tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [self.view addSubview:tableView];
    

    在哪里用你想要的任何位置/大小替换 CGRectMake(...)

    【讨论】:

    • 感谢您的回复。但是我想问,我应该把NSMutableArray放在哪里?谢谢。
    • 您可以将数组存储在视图控制器中并在 UITableViewDataSource 方法中使用它。例如,您将在 tableView:numberOfRowsInSection: 中返回数组的长度。
    • 感谢您的回复。因为 UI 包含其他元素,所以我将 UITableView 添加为它们的一部分。而且我不知道如何调用 UITableDataSource,因为创建它时该类不是表视图类。您介意告诉我如何在数组中导入或添加数据吗?谢谢。
    • 要向UITableView 提供数据,您的视图控制器必须符合UITableViewDataSource protocol。正是通过这些方法返回数据。我建议您下载并查看 Apple 的 TableViewSuite 示例,以了解如何使用表格视图。
    【解决方案2】:

    写完以上代码后,你必须实现 UITableView 的委托方法。

    这些是 UITableView 加载的委托方法

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [your 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] autorelease];
    
        }
    
        // Configure the cell...
        cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
    
        return cell;
    
    }
    

    【讨论】:

    • 感谢您的回复。但我没有使用 UIViewTable 类,我只是使用简单的类。您介意告诉我如何将数组添加到表格或表格单元格吗?谢谢。
    • @MarkSiu 不需要实现 UITableView 类只需编写代码在您的 viewdidload 函数中添加 UITableView 和以上三个函数在您的视图中显示表
    • 感谢您的回复。我在 .m 类中添加了上述函数。但是,我发现这些函数并没有被 UITableView 调用。什么时候调用函数?谢谢。
    • @MarkSiu 你在 viewdidload 中添加这个代码然后那个函数是自动调用 tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease]; tableView.dataSource = self;// 这行调用函数 tableView.delegate = self;// 这行调用函数 [self.view addSubview:tableView];
    • 顺便说一句,如果你像我一样使用 ios6 引入的dequeueReusableCellWithIdentifier:forIndexPath:(diff.method),你也需要提前这个调用:[tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:CellIdentifier];
    【解决方案3】:
    • 创建一个继承自 UIViewController 的新类。
    • 使其符合 UITableViewDataSource 协议。
    • 声明您的表格视图。

    您的头文件应如下所示:

    @interface MyViewController : UIViewController <UITableViewDataSource> {    
    
    }
    
    @property (nonatomic, retain) UITableView *tableView;
    
    @end
    

    在你的类的 viewLoad 方法中:

    • 使用 initWithFrame 创建一个表视图。全高使用 320x460 尺寸。如果您有导航栏,则从高度移除 44;如果您有标签栏,则移除 49。
    • 创建一个新视图。
    • 将表格视图添加到新视图中。
    • 将控制器视图设置为新视图。
    • 将表视图数据源设置为您的实例(自身)。
    • 实现两个必需的数据源方法:tableView:cellForRowAtIndexPath 和 tableView:numberOfRowsInSection

    您的实现文件应如下所示:

    #import "MyViewController.h"
    
    @implementation MyViewController
    
    @synthesize tableView=_tableView;
    
    - (void)dealloc
    {
        [_tableView release];
    
        [super dealloc];
    }
    
    #pragma mark - View lifecycle
    
    - (void)loadView
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0) style:UITableViewStylePlain];
        self.tableView = tableView;
        [tableView release];    
    
        UIView *view = [[UIView alloc] init];
        [view addSubview:self.tableView];
        self.view = view;
        [view release];
    
        self.tableView.dataSource = self;
    }
    
    - (void)viewDidUnload {
        self.tableView = nil;
    
        [super viewDidUnload];
    }
    
    #pragma mark - Table view data source
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *MyCellIdentifier = @"MyCellIdentifier";
    
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
    
        if(cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier] autorelease];
        }
    
        return cell;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       return 5;
    }
    
    @end
    

    【讨论】:

      【解决方案4】:

      也许它对你们所有的新手也有帮助。

      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // init table view
          tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
      
          //or, you may do that 
          //tableView = [[UITableView alloc] init];
          //tableView.frame = CGRectMake:(5 , 5 , 320 , 300);
      
          // must set delegate & dataSource, otherwise the the table will be empty and not responsive
          tableView.delegate = self;
          tableView.dataSource = self;
      
          tableView.backgroundColor = [UIColor cyanColor];
      
          // add to canvas
          [self.view addSubview:tableView];
      }
      
      #pragma mark - UITableViewDataSource
      // number of section(s), now I assume there is only 1 section
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
      {
          return 1;
      }
      
      // number of row in the section, I assume there is only 1 row
      - (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
      {
          return 1;
      }
      
      // the cell will be returned to the tableView
      - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *cellIdentifier = @"HistoryCell";
      
          // Similar to UITableViewCell, but 
          JSCustomCell *cell = (JSCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
          if (cell == nil) {
              cell = [[JSCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
          }
          // Just want to test, so I hardcode the data
          cell.descriptionLabel.text = @"Testing";
      
          return cell;
      }
      
      @end
      

      【讨论】:

      • 对于新手(我),要复制/粘贴到通用应用程序中,将“JSCustomCell”更改为“UITableViewCell”,将“cell.descriptionLabel.text”更改为“cell.textLabel.text”。返回“numberOfRowsInSection”的数组项计数。提醒在 .h 文件“”中声明委托方法(我认为这就是它的名称)。 (谢谢阿贝)
      猜你喜欢
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      相关资源
      最近更新 更多