2015-07-03 38 views
0

在我的表格視圖中,我想在ios中顯示一些標題,所以我可以如何做到這一點。我的代碼在下面給出...因爲我想顯示數據像..... ..... 名稱類RollNo地址 在表視圖然後該怎麼做這..任何人都可以幫助我?表格視圖數據以特定格式顯示?

enter code here 
    deliveryCases = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, 320, 480)]; 
    deliveryCases.backgroundColor = [UIColor clearColor]; 
    deliveryCases.dataSource = self; 
    deliveryCases.delegate = self; 
    [self.view addSubview:deliveryCases]; 

    UIView *headerView0 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100 , 50)]; 
    UILabel *labelView0 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100 , 50)]; 
    labelView0.text = @"Name"; 
    [headerView0 addSubview:labelView0]; 
    deliveryCases.tableHeaderView = headerView0; 


    UIView *headerView1 = [[UIView alloc] initWithFrame:CGRectMake(10, 19, 100 , 50)]; 
    UILabel *labelView1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 19, 100 , 50)]; 
labelView1.text = @"CLass"; 
    [headerView1 addSubview:labelView1]; 
deliveryCases.tableHeaderView = headerView1; 
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell = [deliveryCases  dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell==nil) 
{ 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

} 

     [cell setUserInteractionEnabled:YES]; 
    tableView.allowsSelection=YES; 

     return cell; 



    } 

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
    } 

    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    return 2; 
} 

回答

2

你所做的一切是正確的,將在你的頭一個視圖。所有標籤的正確來源,你會達到你想要的。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    UIView *headerView = [[UIView alloc] initWithFrame:VIEW_FRAME_HERE]; 


    UILabel *lbl1 = [[UILabel alloc] initWithFrame:LABEL_FRAME_HERE]; 
    lbl1.text = @"YOUR_HEADER_NAME"; 
    [headerView addSubview:lbl1]; 

    UILabel *lbl2 = [[UILabel alloc] initWithFrame:LABEL_FRAME_HERE]; 
    lbl2.text = @"YOUR_HEADER_NAME"; 
    [headerView addSubview:lbl2]; 
    . 
    . 
    . 
    return headerView; 
} 

因此,在您的TableViewCell中,您只需向它添加一個子視圖,並使用正確的標籤來源及其值。如果您繼承UITableViewCell並在其中添加子視圖,那將是正確的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *myCell = @"cellID"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell]; 
    if(!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell]; 
    } 

    UIView *cellValues = [[UIView alloc] initWithFrame:CELL_FRAME_HERE]; 


    UILabel *value1 = [[UILabel alloc] initWithFrame:VALUE_FRAME_HERE]; 
    value1.text = @"VALUES_HERE"; 
    [cellValues addSubview:value1]; 

    UILabel *value2 = [[UILabel alloc] initWithFrame:VALUE_FRAME_HERE]; 
    value2.text = @"VALUES_HERE"; 
    [cellValues addSubview:value2]; 
    . 
    . 
    . 
    [cell addSubview:cellValues]; 
    return cell; 
} 
+0

有沒有什麼辦法來添加表視野... ????? –

+0

是的,就像你在你的單元格中添加標籤所做的一樣,只需在你的單元格中添加UITextField ...如果你想確定一個特定的字段,可以在你的字段中添加標籤並通過他們的標籤訪問它們 –