網頁

2012年4月26日 星期四

iOS5 UIViewController Table View

UIViewController 下拉 Table 記得拉藍線 (UITableViewController不用 已幫拉)
注意view拉下拉Table的為@interface ViewController : UIViewController
不是UITableViewController

參照UITableViewController拉UI




拉關係藍線



忘記會出現以下錯誤


預設拉完會有一個警告 提醒Cell identifier要設

這邊相當於告知要客製Cell的name 若在4.3 相當於客製cell xib的name





新建 .h .m檔 一開始選UITableViewController 再進程式修改成UIViewController (不然TableView的函式要自己產生)




.h
  

@interface ViewTable : UIViewController
{
    
    NSMutableArray *yourData;
    
}

.m
  


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    yourData = [NSMutableArray arrayWithObjects:@"xxx",@"xxx",nil];
                
    // 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;
}


#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 [yourData count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    cell.textLabel.text = [yourData objectAtIndex:indexPath.row];
    
    return cell;
}

加入背景圖的方法 .h
  

@interface ViewTable : UIViewController
{
    
    NSMutableArray *yourData;
    
    UIView *bg;
    UITableView *table;       
}
@property (nonatomic , retain) IBOutlet UIView      *bg;
@property (nonatomic , retain) IBOutlet UITableView *table;
@end
.m
  

@implementation ViewTable
@synthesize bg;
@synthesize table;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    yourData = [NSMutableArray arrayWithObjects:@"xxx",@"xxx",nil];
    
    
    
    bg.frame = CGRectMake(0, 0, 320, 480);
    
    UIImage *bodyImage = [UIImage imageNamed: @"content_background_body.png"];
 UIImageView *bodyView = [[UIImageView alloc] initWithImage: bodyImage];
    [bodyView setFrame:self.table.frame]; 
    self.table.backgroundView = bodyView;
    
    // 在UITableViewController很簡單 無需.h UIView UITableView 以下即可
    // UIImage *bodyImage = [UIImage imageNamed: @"content_background_body.png"];
 // UIImageView *bodyView = [[UIImageView alloc] initWithImage: bodyImage];
 // self.tableView.backgroundView = bodyView;

}
同樣記得拉藍線關聯 否則無作用





表格文字改設在Label 拉Label至Table Cell 不用IBOutlet 利用tag識別Label

  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    UILabel *cellLabel = (UILabel *) [cell viewWithTag:2] ;
   
    cellLabel.lineBreakMode = UILineBreakModeWordWrap; //字符換行換行
    cellLabel.numberOfLines = 0; // //設置自動行數
    NSString *text = @"test"; //內容
    [cellLabel setText:text];
    
    UILabel *cellLabel1 = (UILabel *) [cell viewWithTag:1] ;
}


iOS 5 Storyboard + UITableView Tutorial II (Improved)

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。