如何使用Objective c中的自定义单元格在单行中显示三个图像?

时间:2014-01-06 12:01:36

标签: ios uitableview

在UITableView中,在一个单元格中创建一个包含3个图像的自定义单元格并显示图像。功能取决于我的数组计数,但在NSLog中,我没有得到正确的计数... 另外,当调用cellForRowAtIndexPath:时,会调用第一个if条件;它没有输入else if

screen shot of table cell with three images

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
     return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"cell:%f", ceil(((float)[arrData count]) / 3.0));
    return ceil(((float)[arrData count]) / 3.0);

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PhotosCell *cell = (PhotosCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhotosCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if (arrData.count-1 >=indexPath.row * 3) {
        cell.btnFst.tag = indexPath.row+1;
        [cell.btnFst setTitle:[arrData objectAtIndex:(indexPath.row*3)] forState:UIControlStateNormal];
        [cell.btnFst addTarget:self action:@selector(btnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    } else if (arrData.count-1 >=(indexPath.row*3)+1) {
        cell.btnFst.tag = indexPath.row+2;
        [cell.btnFst setTitle:[arrData objectAtIndex:(indexPath.row*3)+1] forState:UIControlStateNormal];
        [cell.btnFst addTarget:self action:@selector(btnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    } else if (arrData.count-1 >=(indexPath.row*3)+2) {
        cell.btnFst.tag = indexPath.row+3;
        [cell.btnFst setTitle:[arrData objectAtIndex:(indexPath.row*3)+2] forState:UIControlStateNormal];
        [cell.btnFst addTarget:self action:@selector(btnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    }

    return cell;
}

3 个答案:

答案 0 :(得分:0)

您可以使用以下方法设计细胞

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // Return the number of rows in the section.
   int numberOfRows = [ARRAY_OF_DATA count]/3;
   if((ARRAY_OF_DATA%3)>0){
       numberOfRows++;
   }

   return numberOfRows;

} 

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier 
{
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ] autorelease];

   CGRect contentRect = CGRectMake(25.0f, 4.0f, 230.0f, 20.0f);
   UIImage *image = [[UIImage alloc]initWithFrame:contentRect]
   image.tag = 1;
   [cell.contentView addSubview:image];
   [image release];

   contentRect = CGRectMake(CHANGE_ACCORDING_YOUR_REQUIREMENT);
   image = [[UIImage alloc]initWithFrame:contentRect]
   image.tag = 2;
   [cell.contentView addSubview:image];
   [image release];

   contentRect = CGRectMake(CHANGE_ACCORDING_YOUR_REQUIREMENT);
   image = [[UIImage alloc]initWithFrame:contentRect]
   image.tag = 3;
   [cell.contentView addSubview:image];
   [image release];

   return cell;
}

//返回要为每一行呈现的单元格

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"Image Cell Identifier";
    cellNew = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    cellNew = [self getCellContentView:MyIdentifier];

    UIImage *img1 = (UIImage *)[cellNew viewWithTag:1];
    UIImage *img2 = (UIImage *)[cellNew viewWithTag:2];
    UIImage *img3 = (UIImage *)[cellNew viewWithTag:3];

     //Write set Image Logic here

     return cellNew; 
}

您也可以使用xib文件执行此操作。您可以为tablecell管理单独的xib。 如果您想知道其他任何事情或有任何疑问,可以在http://chat.stackoverflow.com/rooms/43424/coders-diary

询问

答案 1 :(得分:0)

自定义单元格实现......

@implementation CustomTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        self.profileImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 54, 54)];
        [self.profileImageView1 setBackgroundColor:[UIColor whiteColor]];
        [self.profileImageView1 setClipsToBounds:YES];
        [self.profileImageView1 setContentMode:UIViewContentModeScaleAspectFill];
        [self.profileImageView1.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.3].CGColor];
        [self.profileImageView1.layer setBorderWidth:1];
        [self.profileImageView1.layer setCornerRadius:2];
        [self.contentView addSubview:self.profileImageView1];


        self.profileImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(60, 5, 54, 54)];
        [self.profileImageView2 setBackgroundColor:[UIColor whiteColor]];
        [self.profileImageView2 setClipsToBounds:YES];
        [self.profileImageView2 setContentMode:UIViewContentModeScaleAspectFill];
        [self.profileImageView2.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.3].CGColor];
        [self.profileImageView2.layer setBorderWidth:1];
        [self.profileImageView.layer setCornerRadius:2];
        [self.contentView addSubview:self.profileImageView2];

        self.profileImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(120, 5, 54, 54)];
        [self.profileImageView3 setBackgroundColor:[UIColor whiteColor]];
        [self.profileImageView3 setClipsToBounds:YES];
        [self.profileImageView3 setContentMode:UIViewContentModeScaleAspectFill];
        [self.profileImageView3.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.3].CGColor];
        [self.profileImageView3.layer setBorderWidth:1];  
        [self.profileImageView3.layer setCornerRadius:2];
        [self.contentView addSubview:self.profileImageView3];
    }
    return self;
}


-(void)setThumbnailImages:(UIImage*)image  tag:(int)tagValue
{
    if(tagValue1)
    {
        [self.profileImageView1 setImage:image];
    }
    else if(tagValue2)
    {
        [self.profileImageView2 setImage:image];
    }
    else
    {
        [self.profileImageView3 setImage:image];
    }
}

UITableViewController

如果你只调用setThumbnailImages方法。为自定义tableviewcell类创建委托

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    YourTableViewCell *cell = (YourTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[YourTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.delegate = self;
    [cell setThumbnailImages:[UIImage imageNamed:[timeZone leadImage]]];

    return cell;    
}

答案 2 :(得分:0)

首先创建TableViewCell并创建其XIB并声明其图像 在它之后 在你的viewcontroller声明

NSMutableArray *arr=[[NSMutableArray alloc]init];

for (int i=0; i<5; i++)
{

    UIImage *loader1=[UIImage imageNamed:@"loader_events_1"];

    UIImage *loader2=[UIImage imageNamed:@"loader_events_2"];

    UIImage *loader3=[UIImage imageNamed:@"loader_events_3"];


    NSDictionary *dict = @{"Rat_IMG1":loader1,"Rat_IMG2":loader2,"Rat_IMG3":loader3};
    [arr addObject:dict];
}

    UINib *nib = [UINib nibWithNibName:@"XIB_OF_CELL" bundle:nil];
    CELL_NAME *cell = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
    TABLEVIEW.rowHeight = cell.frame.size.height;

    // Register CustomCell
    [TABLEVIEW registerNib:nib forCellReuseIdentifier:@"XIB_OF_CELL"];

//// Add This Metho


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CELL_NAME *cell = [tableView dequeueReusableCellWithIdentifier:@"XIB_OF_CELL"];

    // Set contents
    cell.imageview1.image = [[arr objectAtIndex:indexPath.section]objectForKey:@"Rat_IMG1"];  
    cell.imageview2.image = [[arr objectAtIndex:indexPath.section]objectForKey:@"Rat_IMG2"];
    cell.imageview3.image = [[arr objectAtIndex:indexPath.section] objectForKey:@"Rat_IMG3"];

    return cell;
}