PubNative integration ios - ios

I integrate pubnative library in my project , but it load only one app in my table view and i don't want tihis. I want to populate my table view with all partener apps that i have. When i lunch the app date are diffrent every time .
-(void)viewDidLoad
{
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
PNAdRequestParameters *parameters = [PNAdRequestParameters requestParameters];
[parameters fillWithDefaults];
parameters.ad_count = #5;
parameters.app_token = #"My app token";
parameters.icon_size=#"400x400";
self.request = [PNAdRequest request:PNAdRequest_Native
withParameters:parameters
andCompletion:^(NSArray *ads, NSError *error)
{
if(error)
{
NSLog(#"Pubnative - Request error: %#", error);
}
else
{
NSLog(#"Pubnative - Request end");
weakSelf.ads = [[NSMutableArray alloc] initWithArray:ads];
weakSelf.model = [ads firstObject];
[self.tableView reloadData];
}
}];
[self.request startRequest];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[PNTableViewManager controlTable:self.tableView];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[PNTableViewManager controlTable:nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *result = nil;
PNBannerTableViewCell *bannerCell = [tableView dequeueReusableCellWithIdentifier:bannerCellID];
if(!bannerCell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"PNBannerTableViewCell"
owner:self options:nil];
bannerCell = [topLevelObjects objectAtIndex:0];
}
bannerCell.model = self.model;
result = bannerCell;
return result;
}

You should then assign a different ad each time
- (UITableViewCell *)tableView:(UITableView *)tableView cell ForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *result = nil;
PNBannerTableViewCell *bannerCell = [tableView dequeueReusableCellWithIdentifier:bannerCellID];
if(!bannerCell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"PNBannerTableViewCell"
owner:self options:nil];
bannerCell = [topLevelObjects objectAtIndex:0];
}
// We assign an add to the table
bannerCell.model = self.ads[indexpath.row % [self.ads count]];
result = bannerCell;
return result;
}

Related

How to resolve when UITableview is double open on same page

I have 2 UITableView. On first UITableView make for search and select cell. After selected cell from UITableView1. It's will show blank data of UITableView2 and show new UITableView2 again with data.
I'm sorry for my bad english language.
I can speak english a little bit.
If you don't understand my question.
Please follow to see pictures below here.
First UITableView: (Click button for go to second UITableView.)
After click button. UITableView2 show blank data.
And auto show new UITableView2 with data again.
UITableView1
#interface SearchByContainerDetailViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableViewWhereHoseList;
#end
#implementation SearchByContainerDetailViewController
#synthesize labelName,strName,txResult,strResult,labelStatus;
NSString *selectedWhereHouse;
GlobalVariable *gloablOnWherehouse;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
labelName.text = strName;
//txResult.text = strResult;
labelStatus.text = #"NULL";
gloablOnWherehouse = [GlobalVariable sharedInstance];
gloablOnWherehouse.arTableDataSelectedWhereHouse = [[NSArray alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (gloablOnWherehouse.arTableDataSelected)?[gloablOnWherehouse.arTableDataSelected count]:1;
}
- (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%lu", (unsigned long)[gloablOnWherehouse.arTableDataSelected count]);
return (gloablOnWherehouse.arTableDataSelected)?[gloablOnWherehouse.arTableDataSelected count]:1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SearchByContainerDetailViewCell";
SearchByContainerDetailViewCell *cell = [self.tableViewWhereHoseList dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SearchByContainerDetailViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
} else {
if (gloablOnWherehouse.arTableDataSelected) {
NSMutableArray *myMutbleArray = [[NSMutableArray alloc] init];
[myMutbleArray addObject:gloablOnWherehouse.arTableDataSelected];
if (myMutbleArray) {
NSDictionary *myDic = [gloablOnWherehouse.arTableDataSelected objectAtIndex:indexPath.row];
NSDictionary *cont = [myDic objectForKey:#"DataList_SPI_Detail"];
NSString *date = [self getString:[cont objectForKey:#"date"]];
NSString *qty = [self getString:[cont objectForKey:#"qty"]];
NSString *stock = [self getString:[cont objectForKey:#"stock"]];
NSString *ord = [self getString:[cont objectForKey:#"ord"]];
NSString *custmr = [self getString:[cont objectForKey:#"custmr"]];
NSString *remarks = [self getString:[cont objectForKey:#"remarks"]];
NSString *invoice = [self getString:[cont objectForKey:#"invoice"]];
NSString *due = [self getString:[cont objectForKey:#"due"]];
NSString *lot = [self getString:[cont objectForKey:#"lot"]];
NSString *gap = [self getString:[cont objectForKey:#"gap"]];
[cell setDate:date setQty:qty setStock:stock setOrd:ord setCustmr:custmr setRemarks:remarks setInvoice:invoice setDue:due setLot:lot setGap:gap];
}
}
}
return cell;
}
- (void) requestEWIServiceFinish:(EWIConnector *)connector responseData:(NSDictionary *)responseData{
NSLog(#"finish %#",connector.serviceName);
NSLog(#"response %#",responseData);
if ([connector.serviceName isEqualToString:#"special_selected_f10"])
{
NSLog(#"finish %#",connector.serviceName);
NSDictionary *content = responseData[#"content"];
NSString *stAlertMes = [content objectForKey:#"alertMessage"];
stAlertMes = [self getString:stAlertMes];
NSLog(#"AlertMSG : %#", stAlertMes);
if (![stAlertMes isEqualToString:#""]) {
NSLog(#"ALERT MESSAGE : %#", stAlertMes);
gloablOnWherehouse.arTableDataSelected = [[NSArray alloc] init];
}
else
{
NSLog(#"HAS DATA");
gloablOnWherehouse.arTableDataSelected = [content objectForKey:#"DataList_SPI_DetailCollection"];
[self.tableViewWhereHoseList reloadData];
labelStatus.text = #"F11";
}
}
else
{
NSLog(#"response %#",responseData);
}
}
- (void) requestEWIServiceFail:(EWIConnector *)connector error:(NSError *)error{
NSLog(#"request fail %#",connector);
}
- (NSString *)getString:(NSString *)string
{
return [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (IBAction)btnf10
{
NSMutableDictionary *content = [NSMutableDictionary dictionary];
[content setValue:[[AppSetting sharedInstance] token] forKey:#"ewitoken"];
//[content setValue:gloablOnWherehouse.selectedWhereHouse forKey:#"model_Name"];
[[EWIConnector connector] requestEWIService:#"special_selected_f10" requestData:content delegate:self];
}
UITableView2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
globlOnDisplayEffect = [GlobalVariable sharedInstance];
globlOnDisplayEffect.arTableDataSelectedWhereHouse = [[NSArray alloc] init];
[self.tableViewDetailList reloadData];
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (globlOnDisplayEffect.arTableDataSelected)?[globlOnDisplayEffect.arTableDataSelected count]: 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"DisplayEffectQtyViewCell";
DisplayEffectQtyViewCell *cell = [self.tableViewDetailList dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DisplayEffectQtyViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
} else {
if (globlOnDisplayEffect.arTableDataSelected) {
NSMutableArray *myMutbleArray = [[NSMutableArray alloc] init];
[myMutbleArray addObject:globlOnDisplayEffect.arTableDataSelected];
if (myMutbleArray) {
NSDictionary *myDic = [globlOnDisplayEffect.arTableDataSelected objectAtIndex:indexPath.row];
NSDictionary *cont = [myDic objectForKey:#"DataList_SPI_DetailF10"];
NSString *f10_cmpt = [self getString:[cont objectForKey:#"f10_cmpt"]];
NSString *f10_dt = [self getString:[cont objectForKey:#"f10_dt"]];
NSString *f10_item = [self getString:[cont objectForKey:#"f10_item"]];
NSString *f10_lot = [self getString:[cont objectForKey:#"f10_lot"]];
NSString *f10_model = [self getString:[cont objectForKey:#"f10_model"]];
NSString *f10_of = [self getString:[cont objectForKey:#"f10_of"]];
NSString *f10_semi = [self getString:[cont objectForKey:#"f10_semi"]];
NSString *f10_tm = [self getString:[cont objectForKey:#"f10_tm"]];
NSString *f10_uncmp = [self getString:[cont objectForKey:#"f10_uncmp"]];
[cell setf10_cmpt:f10_cmpt setf10_dt:f10_dt setf10_item:f10_item setf10_lot:f10_lot setf10_model:f10_model setf10_of:f10_of setf10_semi:f10_semi setf10_tm:f10_tm setf10_uncmp:f10_uncmp];
}
}
}
return cell;
}
- (void) requestEWIServiceStart:(EWIConnector *)connector{
NSLog(#"start %#",connector.endpoint);
}
TO Check if view controller already push or not.
if(![self.navigationController.topViewController isKindOfClass:[NewViewController class]]) {
[self.navigationController pushViewController:newViewController animated:YES];
}

Slide out menu with categories crashes by saying "NSCFString objectAtIndex" while using APExpandableTableView

I am using a third party library to show drop down cells for a slide out menu with an arrow to open the subcategories. Using APExpandableTableView I get an error which says:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x7f8612e834f0".
I don't know what I'm doing wrong.
- (void)viewDidLoad {
[super viewDidLoad];
self.expandableTableView.expandableTableViewDelegate = self;
[self customSetup];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)customSetup
{
newArray = [[NSMutableArray alloc] init];
NSString *apiURL = [NSString stringWithFormat:#"%#/products/categories",mainURL];
OAuth1Legged *OAuth = [[OAuth1Legged alloc] init];
NSString *finalURL = [OAuth generateGetURL:apiURL];
[OAuth getJsonResponse:finalURL success:^(NSDictionary *responseDict)
{
dispatch_async( dispatch_get_main_queue(), ^{
self.dataModelArray = [[NSMutableArray alloc] init];
NSMutableDictionary *productCategories = [[NSMutableDictionary alloc] init];
productCategories = [responseDict objectForKey:#"product_categories"];
NSLog(#"Response %# ", productCategories);
self.responseJSON = responseDict;
if ([self.responseJSON isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment.
dictionaryArray= [self.responseJSON objectForKey:#"product_categories"];
if ([dictionaryArray isKindOfClass:[NSArray class]]){
//Added instrospection as suggested in comment.
for (NSDictionary *dictionary in dictionaryArray) {
CategoryBO *categoryBO = [[CategoryBO alloc] init];
categoryBO.categoryName = [dictionary objectForKey:#"name"];
categoryBO.categoryProductId = [dictionary objectForKey:#"id"];
categoryBO.categoryParentID = [dictionary objectForKey:#"parent"];
[newArray addObject:categoryBO];
}
}
}
NSLog(#"Data Array: %#",newArray);
for(int i = 0; i<[newArray count];i++)
{
NSLog(#"%#",[[newArray objectAtIndex:i] categoryName]);
[self.dataModelArray addObject:[[newArray objectAtIndex:i] categoryName]] ;
}
NSLog(#"Data %#", self.dataModelArray);
[self.expandableTableView reloadData];
});
} failure:^(NSError *error) {
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)expandableTableView:(APExpandableTableView *)tableView numberOfChildrenForGroupAtIndex:(NSInteger)groupIndex {
return [[self.dataModelArray objectAtIndex:groupIndex] count] - 1;
}
- (NSInteger)numberOfGroupsInExpandableTableView:(APExpandableTableView *)tableView {
return [self.dataModelArray count];
}
- (UITableViewCell *)expandableTableView:(APExpandableTableView *)tableView cellForChildAtIndex:(NSInteger)childIndex groupIndex:(NSInteger)groupIndex {
NSString *cellIdentifier = #"SampleChildCell";
UITableViewCell *cell = [self.expandableTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[self.dataModelArray objectAtIndex:groupIndex] objectAtIndex:childIndex + 1];
return cell;
}
- (UITableViewCell *)expandableTableView:(APExpandableTableView *)tableView cellForGroupAtIndex:(NSInteger)groupIndex {
NSString *cellIdentifier = #"SampleGroupCell";
UITableViewCell *cell = [self.expandableTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[self.dataModelArray objectAtIndex:groupIndex] objectAtIndex:0];
return cell;
}
- (void)expandableTableView:(APExpandableTableView *)tableView deleteChildAtIndex:(NSInteger)childIndex groupIndex:(NSInteger)groupIndex {
[[self.dataModelArray objectAtIndex:groupIndex] removeObjectAtIndex:childIndex + 1];
}
- (void)expandableTableView:(APExpandableTableView *)tableView moveChildAtIndex:(NSInteger)sourceIndex toIndex:(NSInteger)destinationIndex groupIndex:(NSInteger)groupIndex {
NSString *moveObject = [[self.dataModelArray objectAtIndex:groupIndex] objectAtIndex:sourceIndex + 1];
[[self.dataModelArray objectAtIndex:groupIndex] removeObjectAtIndex:sourceIndex + 1];
[[self.dataModelArray objectAtIndex:groupIndex] insertObject:moveObject atIndex:destinationIndex + 1];
}
- (void)expandableTableView:(APExpandableTableView *)tableView moveGroupAtIndex:(NSInteger)sourceIndex toIndex:(NSInteger)destinationIndex {
NSMutableArray *moveObject = [self.dataModelArray objectAtIndex:sourceIndex];
[self.dataModelArray removeObjectAtIndex:sourceIndex];
[self.dataModelArray insertObject:moveObject atIndex:destinationIndex];
}

Table View Cells showing blanks

I've got a Table View Cell in .xib. I've got a UILabel and UIImageView in the .xib.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
SchoolsTableViewCell *cell = (SchoolsTableViewCell *) [[[NSBundle mainBundle] loadNibNamed:#"SchoolsTableViewCell" owner:self options:nil] lastObject];
NSLog(#"Before: %#", cell.nameLabel.text);
cell.nameLabel.text = [NSString stringWithFormat:#"Name: %#", [object objectForKey:#"name"]];
NSLog(#"After: %#", cell.nameLabel.text);
PFFile *file = [object objectForKey:#"logo"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
{
cell.logoImageView.image = [UIImage imageWithData:data];
}];
return cell;
}
As you can see, I have 2 NSLog()s, printing the below:
2015-07-08 20:17:26.739 o [1871:108786] Before: (null)
2015-07-08 20:17:26.740 o [1871:108786] After: Name: SMSAS
That is expected.
However, on my Simulator, I can only see a blank cell with Name:. It should show SMSAS in the label, and load a logo, but it's not happening. I can pass the object to the next View Controller when the cell is tapped, and it can show in that View Controller.
This is my .m for the cell.
#implementation SchoolsTableViewCell
- (void)awakeFromNib
{
[super awakeFromNib];
self.nameLabel = [[UILabel alloc] init];
self.logoImageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.logoImageView];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
#end
I'm sure it's a simple mistake. Anyone care to open my eyes? Thanks!
Use dequeueReusableCellWithIdentifier
static NSString * CellIdentifier = #"SchoolsTableViewCell";
SchoolsTableViewCell *cell = (SchoolsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SchoolsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

if-statement ios check json parameter

I'm currently parsing a JSON-feed to my table view in my iOS app. In the JSON-feed there's two different types of posts: "type":"post" and "type":"shared".
When I click on a cell in the Table View, a new view opens with the full posts etc. What I need is to set an if-statement in the didSelectRowAtIndexPath, saying: "If type = post, open this view. Else if type = shared, open this view". So I need to somehow check the json-parameter type. And based on that go to the two different views.
The didSelectRowAtIndexPath method currently looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//getting the data I'm passing to the next view, where movies = NSMutableArray
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = movie;
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
And the full code for parsing the data looks like this:
#implementation HomeViewController
#synthesize profilePic;
#synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
#synthesize btnFaceBook, btnTwitter, btnTwitter2;
#synthesize strURLToLoad;
#synthesize movies;
#synthesize statsHome;
#synthesize fontForCellText;
#synthesize twitterFollowers;
#synthesize facebookLikes;
#synthesize instagramFollowers;
- (void)viewDidLoad
{
[super viewDidLoad];
// Initializing Data Source
movies = [[NSMutableArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"link.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.movies = [[NSArray alloc] initWithArray:JSON];
[self.activityIndicatorView stopAnimating];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
[operation start];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return movies.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"HomeFeedView";
HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HomeFeedView" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = [movie objectForKey:#"message"];
cell.published.text = [movie objectForKey:#"published"];
cell.twitterName.text = [movie objectForKey:#"celebname"];
return cell;
}
- (NSString *)getTextKey
{
return #"message";
}
- (NSString *)getPostedTime
{
return #"published";
}
- (NSString *)getTwitterName
{
return #"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
JSON structure/data:
{
"message":"blablablabla",
"published":"5 hours ago",
"link":"http://google.com",
"unix":1395493814,
"name":"Name",
"story":null,
"picture":"some-pic.jpg",
"type":"shared"
},
{
"message":"blablabla",
"published":"5 hours ago",
"name":"Name",
"unix":1395493814,
"type":"post"
},
In your HomeFeedView.h just add a new property say type.
#property(strong, nonatomic) NSString *type;
Then set this type property in cellForRowAtIndexPath delegate method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"HomeFeedView";
HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HomeFeedView" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = [movie objectForKey:#"message"];
cell.published.text = [movie objectForKey:#"published"];
cell.twitterName.text = [movie objectForKey:#"celebname"];
cell.type = [movie objectForKey:#"type"];
return cell;
}
and didSelect method write this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//getting the data I'm passing to the next view, where movies = NSMutableArray
HomeFeedView *cell = [tableview cellForRowAtIndexPath:indexPath];
if([cell.type isEqualToString: #"post"]){
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = [self.movies objectAtIndex:indexPath.row];
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
else{
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = [self.movies objectAtIndex:indexPath.row];
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
}

showing UIActivityIndicator on fetch of XML

what im trying to do is parse an XML data and show it in my UITableView but the thing is it shows like after 2-3 seconds, im trying to include a UIActivityIndicator when the data loads and also im trying to include a gcd, but the thing is im new at this stuff so im really confuse on what to do or where do i posible put the gcd code.
that is my .m file.
#implementation ViewController
#synthesize listTableView;
dispatch_queue_t myQueue;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[xmlParser listPopulated]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CustomCell";
dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
CustomCellXMLClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomCellXMLClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCellXMLSample" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *nameLabel = [currentData nameOfCat];
NSString *dataToCacheLabel = [myCache objectForKey:nameLabel];
if(nameLabel != nil){
dataToCacheLabel = [NSString stringWithString:nameLabel];
if (dataToCacheLabel != nil) {
[myCache setObject:dataToCacheLabel forKey:nameLabel];
[cell.nameLabel setText:dataToCacheLabel];
}
}
NSString *detailLabel = [currentData descriptionOfCat];
NSString *stringToCache = [myCache objectForKey:detailLabel];
if (detailLabel != nil) {
stringToCache = [NSString stringWithString:detailLabel];
if (stringToCache != nil) {
[myCache setObject:stringToCache forKey:detailLabel];
[cell.detailLabel setText:stringToCache];
}
}
NSString *imageURL = [currentData imageLink];
NSData *dataToCache;
if (imageURL != nil) {
dataToCache = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
if (dataToCache != nil) {
[myCache setObject:dataToCache forKey:imageURL];
[cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
}
else {
NSURL *imageURL = [NSURL URLWithString:#"http://i178.photobucket.com/albums/w255/ace003_album/190579604m.jpg"];
dataToCache = [NSData dataWithContentsOfURL:imageURL];
[myCache setObject:dataToCache forKey:imageURL];
[cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *title = #"Sample View";
return title;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.activityIndicator startAnimating];
self.activityIndicator.hidesWhenStopped = YES;
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myQueue, ^ {
xmlParser = [[XMLParser alloc]loadXMLByURL:#"http://www.irabwah.com/mobile/core.php?cat=0"];
[self.activityIndicator performSelectorOnMainThread:#selector(stopAnimating) withObject:nil waitUntilDone:YES];
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Depending on how XMLParser works your current CGD block is ok for that. You wouldn't usually use performSelectorOnMainThread to return the UI interaction to the main thread but that's just a common convention thing.
Most of your problem could be coming from dataWithContentsOfURL: which you're calling on the main thread in cellForRowAtIndexPath:. You might want to take a look at SDWebImage to help with that, but you could do it yourself with GCD too.
cellForRowAtIndexPath will not be called just because your XML finished loading. In your code block, I suggest before calling stopAnimating that you call reloadData on your UITableView; this will trigger a series of calls, first to numberOfRowsInSection and then to cellForRowAtIndexPath repeatedly. Try putting breakpoints in these functions so you can see the flow of execution.

Resources