代码创建TableView 代码例子

来源:互联网 发布:ppt哪个软件 编辑:程序博客网 时间:2024/06/06 18:31
#import "TableViewController2.h"

@interface TableViewController2 ()

@end

@implementation TableViewController2
@synthesize mytableview;
@synthesize dataList;

- (
id)initWithStyle:(UITableViewStyle)style
{
    
self = [super initWithStyle:style];
    
if (self) {
        
// Custom initialization
    }
    
return self;
}

- (
void)viewDidLoad
{
    [
super viewDidLoad];
    
NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津"nil];
    
self.dataList = list;
    
    
UITableView *tableView = [[UITableView allocinitWithFrame:self.view.frame style:UITableViewStylePlain] ;
    
// 设置tableView的数据源
    
   
    tableView.
dataSource = self;
    
// 设置tableView的委托
    tableView.
delegate = self;
    
// 设置tableView的背景图
  
//  tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
    
    
self.mytableview=tableView;
    
self.mytableview = tableView;
  [
self.view addSubview:mytableview];
    
    
    
// Uncomment the following line to preserve selection between presentations.
    
// self.clearsSelectionOnViewWillAppear = NO;
    
    
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (
void)didReceiveMemoryWarning
{
    [
super didReceiveMemoryWarning];
    
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (
NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    
// Return the number of sections.   //这几个你就看英文吧
    
return  1;
}

- (
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    
// Return the number of rows in the section.
    
return [dataList count];
}


- (
UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
static NSString *CellWithIdentifier = @"Cell";
 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
 
if (cell == nil) {
 cell = [[
UITableViewCell allocinitWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellWithIdentifier];//这里123来设置不同的风格
 }
 
NSUInteger row = [indexPath row];
 cell.
textLabel.text = [self.dataList objectAtIndex:row];
 
//cell.imageView.image = [UIImage imageNamed:@"green.png"];
 cell.
detailTextLabel.text = @"详细信息";
 
return cell;
}
0 0