Que*_*ons 22 iphone objective-c uitableview
我想问一个关于目标C的UITableView的问题.我正在编写一个程序,我想以编程方式创建UI.但是,我不知道如何以编程方式显示表.我已经有一个NSMutableArray来存储显示的数据.我创建了一个对象UITableView *tableData;
,下一步该怎么做?非常感谢你.
Chr*_*ble 43
像这样的东西应该做的伎俩(假设这是从你的视图控制器运行,你有一个属性为表视图设置):
tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
Run Code Online (Sandbox Code Playgroud)
在哪里替换CGRectMake(...)
你想要的任何位置/尺寸.
pri*_*nka 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;
}
Run Code Online (Sandbox Code Playgroud)
小智 21
您的头文件应如下所示:
@interface MyViewController : UIViewController <UITableViewDataSource> {
}
@property (nonatomic, retain) UITableView *tableView;
@end
Run Code Online (Sandbox Code Playgroud)
在您的类的viewLoad方法中:
您的实现文件应如下所示:
#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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
67836 次 |
最近记录: |