ios - How to add image to tableviewcell -
sorry such new question, searched day , still cannot find answer. when run app, shows rows label , count only, image not showing on table view cell.
#import "viewcontroller.h" @interface viewcontroller () <uitableviewdatasource, uitableviewdelegate> { nsmutablearray * imagenamearray; //__weak iboutlet uiimageview *cell; __weak iboutlet uitableview *table; } @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. [self arraysetup]; } -(void) arraysetup { imagenamearray = [nsmutablearray arraywitharray:@[@"2.jpg"]]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection: (nsinteger)section { return imagenamearray.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ static nsstring *cellid = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellid]; //cell.imageview.image = [uiimage imagenamed:imagenamearray [indexpath.row]]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } cell.textlabel.text = imagenamearray [indexpath.row]; cell.detailtextlabel.text = [nsstring stringwithformat:@"%d", (int) indexpath.row + 1]; uiimageview *imv = [[uiimageview alloc]initwithframe:cgrectmake(10,5, 50, 25)]; imv.image=[uiimage imagenamed:@"2.jpg"]; [cell.contentview addsubview:imv]; cell.imageview.image = imv.image; return cell;
}
by default uitableviewcell
has uiimageview
. try this:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ static nsstring *cellid = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellid]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } cell.textlabel.text = imagenamearray [indexpath.row]; cell.detailtextlabel.text = [nsstring stringwithformat:@"%d", (int) indexpath.row + 1]; cell.imageview.image = [uiimage imagenamed:@"2.jpg"]; return cell; }
another approach use own custom tableviewcell.
Comments
Post a Comment