如何在UITableView中添加多个图像和按钮?

时间:2011-08-19 17:35:16

标签: iphone

我想在表格视图中添加多个图像。

我可以使用cell.imageview.image添加一张图片。

但如何添加一个图像。

我还想在所有单元格中添加按钮。

我该怎么做?

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

创建自己的UITableViewCell子类。

答案 2 :(得分:0)

将UITableView与自定义单元格一起使用,加载额外的图像和标签。要获取自定义单元格,请为单元格创建IBOutlet并使用此方法。

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

答案 3 :(得分: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 {


}