2011-12-03 54 views
0

enter image description here如何使UITableView像下圖一樣?

如何使UITableView像上面的圖像?我知道這是分組表類型。但我們如何將圖像+標籤+按鈕添加到節的標題。

我已經試過

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

,但它與CGRectMake(0,0,320,height)開始。

我想就像在圖像中的部分和確切的部分寬度。

在此先感謝。

回答

0

CustomCell.h

#import <UIKit/UIKit.h> 

@interface CustomCell : UITableViewCell{ 
    id delegate; 
    NSIndexPath *indexpath; 
} 
@property(nonatomic,assign) id delegate; 
@property(nonatomic,retain)NSIndexPath *indexpath; 
@property(nonatomic,retain) IBOutlet UIToolbar *Toolbar; 


-(IBAction)SelectorLeft:(id)sender; 
-(IBAction)SelectorRight:(id)sender; 
@end 

customcell.m

#import "CustomCell.h" 
#import <QuartzCore/QuartzCore.h> 
@implementation CustomCell 
@synthesize Toolbar; 
@synthesize delegate,indexpath; 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 
-(IBAction)SelectorLeft:(id)sender{ 
    [delegate perfromselector:@selector(left:) withObject:indexpath]; 
} 
-(IBAction)SelectorRight:(id)sender{ 
    [delegate perfromselector:@selector(left:) withObject:indexpath]; 
} 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

UItbaleView部分

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"identifier"; 
    NSUInteger row = [indexPath row]; 

    if (row == 0) { 
     CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell == nil) { 
      self.Cell = nil; 
      [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
      cell = self.Cell; 
     } 
     cell.Toolbar.clipsToBounds=YES; 
     CALayer *l=cell.Toolbar.layer; 
      // set corner radious 
     [l setCornerRadius:10]; 
      // to apply border on corners 
     [l setBorderColor:[[UIColor clearColor] CGColor]]; 
      // to apply set border width. 
     [l setBorderWidth:5.0]; 

     return cell; 
    }else{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
     } 
     cell.textLabel.text = [NSString stringWithFormat: @"cell %i",row]; 
cell.delegate = self; 
cell.indexpath = indexpath; 
     return cell; 

    } 
    return nil; 
} 

也不要忘記創建Customcell.xib並通過界面生成器中添加工具欄 也tableview中類中創建CustomCell的出口和處理它作爲

0
its easy all what you have to 

    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     NSUInteger row = [indexPath row]; 
     if (row % 2 == 0) { 
      static NSString *identifier = @"RowOne"; 
      UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
      if (cell == nil) { 
      cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease]; 
      } 
      cell.Title.text = [datasource objectatindex:row]; 
      cell.Title.font = [UIFont fontWithName:@"Tahoma" size:16]; 
cell.contentView.backgroundColor = [UIColor redColor]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
cell.textLabel.textAlignment = UITextAlignmentRight; 
      return cell; 
     }else if (row % 2 == 1) { 
      static NSString *identifier = @"RowTwo"; 
      UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
      if (cell == nil) { 
      cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease]; 
      } 
cell.contentView.backgroundColor = [UIColor redColor]; 
      cell.Title.text = [datasource objectatindex:row]; 
      cell.Title.font = [UIFont fontWithName:@"Tahoma" size:16]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
cell.textLabel.textAlignment = UITextAlignmentRight; 
      return cell; 
     } 
     return nil; 
    } 
+0

排%以上2 == 0將使每個單元格的顏色增加一個,然後您可以更改[uicolor redcoloer]以獲取所需的顏色... –

+0

哥們兒忘了爲第一行添加代碼(右邊有按鈕的黑色行)。這是該部分的第一行。 – samfisher

+0

感謝您的回覆,但您誤解了我的問題。您可以在圖像中看到部分0啓動時的第一行棕色圖像,並在右側標有「短信列表」和「新建」按鈕。我要這個。 – iDilip

2

,而不是試圖改變節頭視圖,您可能希望創建一個自定義單元格用棕色背景,一個標籤和一個按鈕,並使用它的第一行。所以,在-cellForRowAtIndexPath,你可以不喜歡

if (0 == indexPath.row) { 
    return brownCell; 
} else { 
    return normalCell; 
} 

有幾種方法來創建自定義的細胞,我總是從Table View Programming Guide for iOS開始。

0
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
     //create btn left each on handeled by selector 
    UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithTitle:@"List of sms" style:UIBarButtonItemStylePlain target:self action:@selector(ListClicked:)]; 
    //create btn right each on handeled by selector 
    UIBarButtonItem *btnright = [[UIBarButtonItem alloc] initWithTitle:@"New" style:UIBarButtonItemStyleBordered target:self action:@selector(NewClicked:)]; 
     //create uitoolbar then add btns to it as list of array 
    UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
     //change style of toolbar to be black 
    [tool setBarStyle:UIBarStyleBlack]; 
    tool.items = [NSArray arrayWithObjects:btnleft,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],btnright, nil]; 

     //this is the parent view that we will add on it the uitoolbar objects and the buttons 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    [view autorelease]; 
    [view addSubview:tool]; 

    return view; 
} 
-(void)ListClicked:(id)sender{ 
     //handle here btn left 
} 
-(void)NewClicked:(id)sender{ 
    //handle here btn right 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return 44; 
} 

,如果你想在細胞不被你可以在cellforrow在指數路徑檢查第一行頭...... 此外,如果你想這樣做的另一種風格,你可以自定義單元格,並添加它也給的cellForRowAtIndexPath

相關問題