2015-02-19 3 views
-1

Я создал ячейку таблицы, и я хочу добавить изображение с левой стороны. Мне нужно предоставить все ячейки, которые я создал.Добавление изображений в виде таблицы с левой стороны

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    if (cell==nil) { 
     cell=[[UITableViewCell alloc]init]; 
    } 

    if([indexPath row] == 0){ 
     [email protected]"mano"; 
     cell.backgroundColor = [UIColor redColor]; 

    } 
    if([indexPath row] == 1){ 
     [email protected]"happy"; 
     cell.backgroundColor=[UIColor whiteColor]; 
    } 
    if([indexPath row] == 2){ 
     [email protected]"welcome"; 
     cell.backgroundColor=[UIColor grayColor]; 
    } 
    if([indexPath row] == 3) 
    { 
     [email protected]"enjoy"; 
     cell.backgroundColor=[UIColor magentaColor]; 
    } 
     return cell; 
} 
+0

Дорогой, В первой строке у упоминал, что и создали пользовательский класс для ячейки и как он работает с классом UITableViewCell, вы должны определить объект ячейки пользовательского класса. –

+0

no i hav создал только табличное изображение. –

+0

Как вы уже упоминали в первой строке, я дал ans в соответствии с пользовательской ячейкой. Все в порядке, я обновил свой ответ. –

ответ

2

В cellForRowAtIndexPath метод

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

, а затем

NSString *file = [[NSBundle mainBundle] pathForResource:[self.photos objectAtIndex:self.currentIndex] ofType:nil]; 

cell.imageView.image = [UIImage imageWithContentsOfFile:file] 

На месте [UIImage imageNamed: @ ""] использование [UIImage imageWithContentsOfFile:].

Потому что ImageNamed занимает слишком много памяти (проверьте эту ссылку: - http://www.jorambarrez.be/blog/2012/04/19/ios-imagenamed-vs-imagewithcontentsoffile/).

Надеюсь, это поможет вам.

2

Положить в cellforrowatindexpath.

cell.imageView.image = [UIImage imageNamed:@"imageName.png"]; 
+0

да, я понял, ... спасибо u @ ashforlos, @ anbu @, sumit, @ shah –

0

использовал этот код

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

if (cell==nil) { 
    cell=[[UITableViewCell alloc]init]; 
} 



if([indexPath row] == 0){ 
    [email protected]"mano"; 
    cell.backgroundColor = [UIColor redColor]; 
    cell.imageView.image = [UIImage imageNamed:@"arrow0.png"]; 

} 
if([indexPath row] == 1){ 
    [email protected]"happy"; 
    cell.backgroundColor=[UIColor whiteColor]; 
    cell.imageView.image = [UIImage imageNamed:@"arrow1.png"]; 

} 
if([indexPath row] == 2){ 
    [email protected]"welcome"; 
    cell.backgroundColor=[UIColor grayColor]; 
    cell.imageView.image = [UIImage imageNamed:@"arrow2.png"]; 

} 
if([indexPath row] == 3) 
{ 
    [email protected]"enjoy"; 
    cell.backgroundColor=[UIColor magentaColor]; 
cell.imageView.image = [UIImage imageNamed:@"arrow3.png"]; 
} 
    return cell; 
} 

ImageView является выход UIImageView

0

Прежде всего, чтобы отобразить изображение в виде таблицы, нет необходимости использовать пользовательскую ячейку. Сначала сделайте свой tableviewcell основным. Назначьте идентификатор повторного использования tableviewcell. Написать этот код для tableviewcell

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

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"]; 

    } 

      cell.imageView.image = [UIImage imageNamed:@"image.png"]; 
      cell.backgroundColor=[UIColor magentaColor]; 

      return cell; 

} 

If you have used storyboard then no need to check if cell is nil. It intializes cell automatically when dequeing is not possible. Please check this tutorial. 

http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1

Смежные вопросы