2010-07-26 111 views
22

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

回答

38

写完上面的代码后,你必须实现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; 

} 
+0

感谢您的回复。但是我没有使用UIViewTable类,我只是使用简单的类。你介意告诉我如何将数组添加到表格或表格单元格吗?谢谢。 – Questions 2010-07-26 10:03:56

+0

@MarkSiu没有必要实现UITableView类只需编写代码在你的viewdidload函数中添加UITableView,并在你的视图 – priyanka 2010-07-26 11:42:43

+0

感谢您的回复。我在.m类中添加了上述函数。但是,我发现这些函数没有被UITableView调用。何时调用函数?谢谢。 – Questions 2010-07-27 01:02:55

43

像这样的东西应该做的伎俩(假设这是从您的视图控制器内运行,你有一个属性设置为表视图):如果你与任何位置/大小更换CGRectMake(...)

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

[self.view addSubview:tableView]; 

你想。

+0

感谢您的回复。但我想问一下,我应该在哪里放置NSMutableArray?谢谢。 – Questions 2010-07-26 09:31:18

+0

您可以将数组存储在您的视图控制器中,并在UITableViewDataSource方法中使用它。例如,您将返回tableView:numberOfRowsInSection:中的数组长度。 – 2010-07-26 09:46:30

+0

感谢您的回复。由于UI包含其他元素,因此我将UITableView添加为其中的一部分。我不知道如何调用UITableDataSource,因为在创建它时类不是表视图类。你介意告诉我如何导入或添加数组中的数据?谢谢。 – Questions 2010-07-26 09:59:07

21
  • 创建从继承的UIViewController一个新的类。
  • 使其符合UITableViewDataSource协议。
  • 声明您的表格视图。

你的头文件应该是这样的:

@interface MyViewController : UIViewController <UITableViewDataSource> {  

} 

@property (nonatomic, retain) UITableView *tableView; 

@end 

在你的类的viewLoad方法:

  • 创建使用initWithFrame表视图。在整个高度上使用尺寸320x460。如果您有导航栏,请从高度删除44;如果您有导航栏,请删除44。
  • 创建一个新视图。
  • 将表格视图添加到新视图。
  • 将控制器视图设置为新视图。
  • 将表视图数据源设置为您的实例(self)。
  • 实现两个必需的数据源方法:的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 
2

也许它也为大家谁在新的有用的那里。

- (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 
+0

为新人(我),复制/粘贴到通用应用程序,更改'JSCustomCell'为'UITableViewCell',更改'cell.descriptionLabel.text'为'cell.textLabel.text'。返回'numberOfRowsInSection'的数组项的数量。提醒在.h文件中声明委托方法(我认为这就是所谓的),''。 (谢谢Abhay) – tmr 2015-04-06 23:06:17

相关问题