iphone開發之UITableView 札記

代碼創建控件
UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake:(0,0,320,480)
                                                        style:UITableViewStylePlain];
myTableView.delegate = self;
myTableView.dataSource = self;
[self.view addSubView:myTableView];
//myArr 爲一維數組
Methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
                                         //有四種格式可以選擇
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:identifier]autorelease];
    }
    NSInteger row = [indexPath row];
    cell.textLabel.text = [myArr objectAtIndex:row];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
   
    //後面要添加的內容從文件中讀取
    if (row == 0) {
        cell.detailTextLabel.text = @"detail info";
    }
   
    return cell;
}
//設定每個cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
//設定Header的標題內容,不論tableView是plain還是group都有header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"詳細內容";
}
//設定tableView與上邊界的距離
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
//在每個section上方創建一個view,可放任何控件,注意section個數
//plain和group的區別:樣式爲group時headerView跟隨tableView一起上升,plain時滑動到底部時纔會上升,底部的時候沒試驗...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

  
 UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor blackColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:16];
    //轉屏幕的時候設置標題位置,如不需要轉屏,可直接設置frame
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationPortraitUpsideDown || orientation == UIInterfaceOrientationPortrait)
    {
        headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
    }
    else {
        headerLabel.frame = CGRectMake(30.0, -10.0, 300.0, 44.0);
    }
    headerLabel.text = @"標題";
    [customView addSubview:headerLabel];
    return customView;
}
//二維數組創建與顯示,多個section,cell個數不同
    array = [[NSArray arrayWithObjects:
                   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章