2014-01-06 1 views
-2

В UITableView создайте одну настраиваемую ячейку с 3 изображениями в одной ячейке и отобразите изображения. Функциональность зависит от моего массива, но в NSLog я не получаю правильный счет ... также, когда вызывается cellForRowAtIndexPath:, вызывается первое условие if; он не входит в else if.Как показать три изображения в одной строке с помощью пользовательской ячейки в Objective c?

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; 
} 
+5

что ваш вопрос? –

+0

Прежде всего, ваш вопрос не ясен, но может быть его работой для вас, поэтому положите только условие 'if' remove' else if' .. – iPatel

+0

Я думаю, что ceil возвращает double (ceilf возвращает float), а numberOfRowsInSection должен возвращать NSInteger (не double или float) – Bamsworld

ответ

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-файл. Вы можете управлять отдельным xib для tablecell. Если вы хотите узнать что-нибудь еще, или есть какие-либо сомнения, вы можете задать в пользовательской реализации ячейки http://chat.stackoverflow.com/rooms/43424/coders-diary

+0

PLZ дайте мне пример этого кода. –

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;  
} 
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; 
} 
Смежные вопросы