ios - UITable inside Scroll View or a Table View Header? -
i'm building app parse data uitableview
. using custom uitableviewcell
class.
i want add uiimage
before , on top of uitableview
, uiimage
down below. question if should go head , make custom uitableview
header view containing uiimage
? or if should put uitableview
, uiimageview
inside uiscrollview
?
and how can that?
when scroll in uitableview
want uiimage
on top disappear, if row in table view.
image (john mayer on tour app:
i appreciate solutions!
thanks.
edit:
here's complete .m-code:
#import "demosecondviewcontroller.h" #import "demonavigationcontroller.h" #import "postsobject.h" #import "rnblurmodalview.h" #import "afnetworking.h" #import "postsnextview.h" #import "kiimagepager.h" #import "tableheaderview.h" #import "demomenuviewcontroller.h" #import "uiviewcontroller+refrostedviewcontroller.h" @interface demosecondviewcontroller () <kiimagepagerdelegate, kiimagepagerdatasource> { iboutlet kiimagepager *_imagepager; } @end @implementation demosecondviewcontroller @synthesize tableview = _tableview, activityindicatorview = _activityindicatorview; @synthesize fontforcelltext; @synthesize btnfacebook, btntwitter, btntwitter2; @synthesize strurltoload; @synthesize movies; - (ibaction)showbuttonmenu { [self.frostedviewcontroller presentmenuviewcontroller]; } - (void)refresh:(uirefreshcontrol *)refreshcontrol { [refreshcontrol endrefreshing]; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. uirefreshcontrol *refreshcontrol = [[uirefreshcontrol alloc] init]; [refreshcontrol settintcolor:[uicolor greencolor]]; [refreshcontrol addtarget:self action:@selector(refresh:) forcontrolevents:uicontroleventvaluechanged]; [self.tableview addsubview:refreshcontrol]; strurltoload = [[nsmutablestring alloc] init]; [btnfacebook settitle:@"link-1" forstate:uicontrolstatedisabled]; [btntwitter settitle:@"link-2" forstate:uicontrolstatedisabled]; [btntwitter2 settitle:@"link-3" forstate:uicontrolstatedisabled]; [btnfacebook setbackgroundimage:[uiimage imagenamed:@"tab_selected.png"] forstate:uicontrolstatenormal]; [btnfacebook setbackgroundimage:[uiimage imagenamed:@"tab_unselected.png"] forstate:uicontrolstateselected]; [btntwitter setbackgroundimage:[uiimage imagenamed:@"tab_selected.png"] forstate:uicontrolstatenormal]; [btntwitter setbackgroundimage:[uiimage imagenamed:@"tab_unselected.png"] forstate:uicontrolstateselected]; [btntwitter2 setbackgroundimage:[uiimage imagenamed:@"tab_selected.png"] forstate:uicontrolstatenormal]; [btntwitter2 setbackgroundimage:[uiimage imagenamed:@"tab_unselected.png"] forstate:uicontrolstateselected]; nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"postsobject" owner:self options:nil]; postsobject *cell = [nib objectatindex:0]; fontforcelltext = cell.title.font; celltextwidth = cell.title.frame.size.width; cellheightexcepttext = cell.frame.size.height - cell.title.frame.size.height; [self.navigationcontroller setnavigationbarhidden:yes]; self.tableview.separatorcolor = [uicolor clearcolor]; // setting activity indicator view self.activityindicatorview = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylewhitelarge]; self.activityindicatorview.color = [uicolor greencolor]; self.activityindicatorview.hideswhenstopped = yes; self.activityindicatorview.center = self.view.center; [self.view addsubview:self.activityindicatorview]; [self.activityindicatorview startanimating]; self.tableview.separatorcolor = [uicolor clearcolor]; // initializing data source movies = [[nsmutablearray alloc] init]; [self btnfromtabbarclicked:btnfacebook]; } -(uistatusbarstyle)preferredstatusbarstyle{ return uistatusbarstylelightcontent; } - (void)loadjsonfromcurrenturl { [self.activityindicatorview startanimating]; nsurlrequest *request = [[nsurlrequest alloc] initwithurl:[nsurl urlwithstring:strurltoload]]; afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { [movies setarray:json]; [self.activityindicatorview stopanimating]; [self.tableview reloaddata]; } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { nslog(@"request failed error: %@, %@", error, error.userinfo); }]; [operation start]; } - (ibaction)btnfromtabbarclicked:(uibutton *)sender { //unselect 3 buttons btnfacebook.selected = btntwitter.selected = btntwitter2.selected = no; //select button clicked sender.selected = yes; //set string of nsmutablestring property called strurltoload url //the url pre stored in text of uibutton in disabled text. [strurltoload setstring:[sender titleforstate:uicontrolstatedisabled]]; //load url [self loadjsonfromcurrenturl]; } // how can set if-statement here? if section 0, return 1 row, otherwise, if (movies) function... - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (movies && movies.count) { return movies.count; } else { return 0; } } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { if (indexpath.section == 0) { static nsstring *identifier1 = @"tableheaderview"; // cell type 1 tableheaderview *cell = [tableview dequeuereusablecellwithidentifier:identifier1]; if (cell == nil) { nsarray *nib= [[nsbundle mainbundle] loadnibnamed:@"tableheaderview" owner:self options:nil]; cell = (tableheaderview *)[nib objectatindex:0]; } //set image on cell return cell; } else { static nsstring *identifier2 = @"postsobject"; // cell type 2 postsobject *cell = [tableview dequeuereusablecellwithidentifier:identifier2]; if (cell == nil) { nsarray *nib= [[nsbundle mainbundle] loadnibnamed:@"postsobject" owner:self options:nil]; cell = (postsobject *)[nib objectatindex:0]; } nsdictionary *movie = [self.movies objectatindex:indexpath.row]; nsstring *strtext = [movie objectforkey:[self gettextkey]]; cgrect rect = cell.title.frame; rect.size.height = [self getheightfortext:strtext]; cell.title.frame = rect; cell.title.text = strtext; cell.arrow.center = cgpointmake(cell.arrow.frame.origin.x, rect.origin.y + rect.size.height/2); cell.published.text = [movie objectforkey:[self getpostedtime]]; cell.twittername.text = [movie objectforkey:[self gettwittername]]; return cell; } } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { [tableview deselectrowatindexpath:indexpath animated:yes]; nsdictionary *selectedmovie = self.movies[indexpath.row]; postsnextview *nextvc = [[postsnextview alloc] initwithdictionary:movies]; [self.navigationcontroller pushviewcontroller:nextvc animated:yes]; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { nsdictionary *movie = [self.movies objectatindex:indexpath.row]; nsstring *strtext = [movie objectforkey:[self gettextkey]]; cgfloat cellheight = cellheightexcepttext + [self getheightfortext:strtext]; return cellheight; } - (nsstring *)gettextkey { return btntwitter.selected?@"tweet":@"message"; } - (nsstring *)getpostedtime { return btntwitter.selected?@"posted":@"published"; } - (nsstring *)gettwittername { return btntwitter2.selected?@"user":@"celebname"; } - (cgfloat)getheightfortext:(nsstring *)strtext { cgsize constraintsize = cgsizemake(celltextwidth, maxfloat); cgsize labelsize = [strtext sizewithfont:fontforcelltext constrainedtosize:constraintsize linebreakmode:nslinebreakbywordwrapping]; nslog(@"labelsize.height = %f",labelsize.height); return labelsize.height; } @end
you answered own question when
i want image on top disappear, if row in table view
the image should row in tableview. best set different section, image in section #0, rest in section #1.
you going create different type of cells, 1 have breaking news, 1 image.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { if (indexpath.section == 0) { static nsstring *identifier1 = @"celltype1"; // cell type 1 celltype1 *cell = [tableview dequeuereusablecellwithidentifier:identifier1]; if (cell == nil) { nsarray *nib= [[nsbundle mainbundle] loadnibnamed:@"celltype1" owner:self options:nil]; cell = (celltype1 *)[nib objectatindex:0]; } //set image on cell return cell; } else { static nsstring *identifier1 = @"celltype2"; // cell type 2 celltype2 *cell = [tableview dequeuereusablecellwithidentifier:identifier2]; if (cell == nil) { nsarray *nib= [[nsbundle mainbundle] loadnibnamed:@"celltype2" owner:self options:nil]; cell = (celltype2 *)[nib objectatindex:0]; } // set cell breaking news stuff return cell; } }
make sure have number of sections set
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; }
also make sure use different cell identifiers each cell type
and 1 row in first section, make sure set accordingly
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (section==0) { return 1; } else { return breaking_news_count; } }
Comments
Post a Comment