2011-08-19 86 views

回答

0

制作属于你自己的UITableViewCell子类。

+0

我在iphone很新。所以你可以给我更多的想法吗? – Dev

0

使用带有自定义单元格的UITableView以及任何想要的内容,加载额外的图像和标签。要获得自定义单元格,请为该单元格创建一个IBOutlet并使用此方法。

[[NSBundle mainBundle] loadNibNamed:@“customCellView”owner:self options:nil]; 要创建单元格,请创建一个空白的新Nib/Xib文件,使文件拥有该单元格的类,拖出一个UITableviewcell对象并将所需的任何对象放在该视图顶部,将背景设置为清晰颜色,以及何时您加载笔尖,将所有信息输入到这些图像和标签中。 GL

0

这是我在应用程序中使用了很多代码块。它并不是最快的实现方式,但它可以让您完全控制单元的外观和内容。使用下面的代码可以看到我的应用程序的外观。一个单元格上有2个按钮。他们在推送时都会做不同的事情,如果选择了实际的CELL,我还会做其他事情。我删除了一些代码,因为我确定你不关心我在选择单元格时做什么,只知道如何获取按钮,并知道哪个按钮被按下。

enter image description here

-(void)scheduleServiceButtonPressed:(id)sender { 

//when the user click the button "service appt" on a table cell, we get the cellRow and use that to identify which car to show 
    ServiceApptViewController * vc = (ServiceApptViewController *)[[ServiceApptViewController alloc] init]; 
    vc.cameFromGarageSelectionInt = [sender tag]; // here is the cell row that was selected. 
    [self.navigationController pushViewController:vc animated:YES]; 
    [vc release]; 


} 


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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return 70; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [app.savedUserVehicleArray count]; 

} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 

    cell = [self getCellContentView:CellIdentifier:indexPath.row]; 
    [cell setBackgroundColor:[UIColor redColor]]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    NSLog(@"out cells for index"); 

    return cell; 
} 

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier:(int)cellIndex { 

    NSLog(@"in content"); 

    vehicleForRow = [app.savedUserVehicleArray objectAtIndex:cellIndex]; 

    //CGRect CellFrame = CGRectMake(0, 0, 300, 60); 
    CGRect CellFrame = CGRectMake(0, 0, 320, 70); 
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease]; 


    // put a UIView underneath for coloring 
    UIView * view = [[UIView alloc] initWithFrame:CellFrame]; 

    if (cellIndex%2 == 0){ 
     view.backgroundColor = [UIColor whiteColor]; 
    }else{ 
     //view.backgroundColor = [UIColor colorWithRed:0.98 green:0.92 blue:0.52 alpha:1.0]; 
     view.backgroundColor = [UIColor colorWithRed:.238 green:.238 blue:0.238 alpha:.10]; 
    } 
    [cell.contentView addSubview:view]; 
    [view release]; 


    if (vehicleForRow.isDefault && [vehicleForRow.isDefault compare:@"YES"]==0) { 

     //add green check mark if vehicle is default 
     UIImageView * bgimage3 = [[UIImageView alloc] initWithFrame:CGRectMake(245, 15, 40, 32)]; 
     bgimage3.image = [UIImage imageNamed:@"greenCheckMark.png"]; 
     [cell.contentView addSubview:bgimage3]; 
     [bgimage3 release]; 

     //default vehicle label 
     UILabel *lblTemp; 
     NSString * z = [NSString stringWithFormat:@"Default"]; 
     NSString * s1 = z; 
     CGRect Label1Frame = CGRectMake(240, 43, 250, 25); // name 
     lblTemp = [[UILabel alloc] initWithFrame:Label1Frame]; 
     lblTemp.adjustsFontSizeToFitWidth = TRUE; 
     lblTemp.text = s1; 
     lblTemp.font = [UIFont boldSystemFontOfSize:12]; 
     lblTemp.textColor = [UIColor blueColor]; 
     lblTemp.backgroundColor = [UIColor clearColor]; 
     [lblTemp setTextAlignment:UITextAlignmentLeft]; 
     [cell.contentView addSubview:lblTemp]; 
    } 
    else { 
     UIImageView * bgimage3 = [[UIImageView alloc] initWithFrame:CGRectMake(250, 15, 30, 24)]; 
     bgimage3.image = [UIImage imageNamed:@"grayCheckMark.png"]; 
     [cell.contentView addSubview:bgimage3]; 
     [bgimage3 release]; 

     UILabel *lblTemp; 
     NSString * z = [NSString stringWithFormat:@"Set As Default"]; 
     NSString * s1 = z; 
     CGRect Label1Frame = CGRectMake(233, 38, 250, 25); // name 
     lblTemp = [[UILabel alloc] initWithFrame:Label1Frame]; 
     lblTemp.adjustsFontSizeToFitWidth = TRUE; 
     lblTemp.text = s1; 
     lblTemp.font = [UIFont boldSystemFontOfSize:8]; 
     lblTemp.textColor = [UIColor grayColor]; 
     lblTemp.backgroundColor = [UIColor clearColor]; 
     [lblTemp setTextAlignment:UITextAlignmentLeft]; 
     [cell.contentView addSubview:lblTemp]; 
    } 


    // add service button to each cell 
    UIImage *image; 
    schedServiceButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    image = [UIImage imageNamed:@"tableServiceButton.png"]; 
    [schedServiceButton setBackgroundImage:image forState:UIControlStateNormal]; 
    schedServiceButton.frame = CGRectMake(5, 30, 97, 35); 
    [schedServiceButton setTag:cellIndex];//this is how we know which cell button was pressed 
    [schedServiceButton addTarget:self action:@selector(scheduleServiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    schedServiceButton.titleLabel.font = [UIFont systemFontOfSize:12]; 

    [schedServiceButton.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap]; 
    [schedServiceButton setTitle:@"Schedule\nService Appt." forState:UIControlStateNormal]; 
    schedServiceButton.titleLabel.textAlignment = UITextAlignmentCenter; 


    [cell.contentView addSubview:schedServiceButton]; 

    //yes add owners manual button 
     viewOMButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     image = [UIImage imageNamed:@"tableOMButton.png"]; 
     [viewOMButton setBackgroundImage:image forState:UIControlStateNormal]; 
     viewOMButton.frame = CGRectMake(105, 30, 97, 35); 
     [viewOMButton setTag:cellIndex]; 
     [viewOMButton addTarget:self action:@selector(viewOwnersManualButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     viewOMButton.titleLabel.font = [UIFont systemFontOfSize:12]; 

     [viewOMButton.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap]; 
     [viewOMButton setTitle:@"View\nOwner's Manual" forState:UIControlStateNormal]; 
     viewOMButton.titleLabel.textAlignment = UITextAlignmentCenter; 


     [cell.contentView addSubview:viewOMButton]; 




    //car description label 
    UILabel *lblTemp; 
    NSString * z = [NSString stringWithFormat:@"%@ %@ %@",vehicleForRow.userVehicleYear,vehicleForRow.userVehicleMake,vehicleForRow.userVehicleModel]; 
    NSString * s1 = z; 
    CGRect Label1Frame = CGRectMake(10, 5, 250, 25); // name 
    //Initialize Label with tag 1. 
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame]; 
    lblTemp.adjustsFontSizeToFitWidth = TRUE; 
    lblTemp.text = s1; 
    lblTemp.font = [UIFont boldSystemFontOfSize:16]; 
    lblTemp.textColor = [UIColor blueColor]; 
    lblTemp.backgroundColor = [UIColor clearColor]; 
    [lblTemp setTextAlignment:UITextAlignmentLeft]; 
    [cell.contentView addSubview:lblTemp]; 



    return cell; 

} 




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 


} 



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 


}