2016-11-11 3 views
1

Ich benutze Anzeigen innerhalb einer UITableView, wobei jeder vierte Artikel in der Liste eine Anzeige ist. Es funktioniert, aber die Anzeige nimmt die Position eines Objekts wie 1-2-3-ad-5-6-7-ad nicht 1-2-3-ad-4-5-6-adIch verwende Anzeigen in einem uitableview, wobei jeder vierte Artikel in der Liste eine Anzeige ist.

Meine UITableView Methoden

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section==0) 
    { 
      return 0; 
    } 
    else{ 
     return [self.articlesArray count]; 
    } 
} 

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

    NSInteger row = [indexPath row]; 
    if (3 == (row % 4)) { // or 0 == if you want the first cell to be an ad! 
     static NSString *MyIdentifier = @"AdCell"; 
     AdViewCell *cell = (AdViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
     if ((cell == nil) || (![cell isKindOfClass: AdViewCell.class] { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AdCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
      cell = [[AdViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:MyIdentifier] ; 
     } 
     GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeMediumRectangle]; 

     bannerView =[[GADBannerView alloc] initWithFrame:CGRectMake(width,heigth,300,250)]; 

     bannerView.adUnitID [email protected]""; 
     bannerView.rootViewController =self; 
     GADRequest *request = [GADRequest request]; 
     [bannerView loadRequest:request]; 

     [cell.contentView addSubview:bannerView]; 

     return cell; 
    } 
    else { 
     static NSString *simpleTableIdentifier = @"ArticleCell"; 
     ArticleViewCell *cell = (ArticleViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     if ((cell == nil) || (![cell isKindOfClass: ArticleViewCell.class])) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticleCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:1]; 
      cell = [[ArticleViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:simpleTableIdentifier] ; 
     } 

     NSInteger offset = indexPath.row/4; 
     NSInteger roww = indexPath.row - offset; 
     NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:roww]; 

     NSString *imageUrl = [[self.articlesArray objectAtIndex:indexPath.row]objectForKey:@"featured_image_thumbnail_tab_url"]; 
     imageUrl = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl ] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
     if (image) { 
      // Set your image over here 
     }else{ 
      //something went wrong 
      NSLog(@"Error occured : %@", [error description]); 
     } 
    }]; 

    NSString * title=[tempDictionary valueForKeyPath:@"title.rendered"]; 

    cell.titleLabel.text = title; 

    NSString * excerpt=[tempDictionary valueForKeyPath:@"excerpt.rendered"]; 
    NSString *excerpt_rend =[self convertHTML:excerpt]; 
    cell.excerptLabel.text = excerpt_rend; 

    return cell; 
    } 
} 

#pragma mark - Navigation 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"showarticle"]){ 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    DGArticleViewController *articleViewController = (DGArticleViewController *)segue.destinationViewController; 
    articleViewController.articlesDetails = [self.articlesArray objectAtIndex:rowww]; //rowww = roww 
    NSLog(@"data article %@",articleViewController.articlesDetails); 
} 
} 

Antwort

1

führen eine der Anzahl der angezeigten Anzeigen-Offset:

NSInteger offset = indexPath.row/4 
NSIngteger adjustedRow = indexPath.row - offset 
// use row from adjustedRow, instead of indexPath.row 
NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:adjustedRow]; 
... 
NSString *imageUrl = [[self.articlesArray objectAtIndex:adjustedRow]objectForKey:@"featured_image_thumbnail_tab_url"]; 

Auch numberOfRowsInSection: Bedürfnisse angepasst werden, Otherwize Sie fehlende Elemente:

return [self.articlesArray count] + [self.articlesArray count]/4; 

Und in prepareForSegue:

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
NSInteger *adjusted = indexPath.row - indexPath.row/4 
... 
articleViewController.articlesDetails = [self.articlesArray objectAtIndex:adjusted]; 
... 

Test:

for (int testIndexPathRow = 0; testIndexPathRow < 100; ++testIndexPathRow) { 
    if (3 == testIndexPathRow % 4) { 
     NSLog(@"Ad"); 
    } else { 
     NSInteger offset = testIndexPathRow/4; 
     NSInteger row = testIndexPathRow - offset; 
     NSLog(@"data at index: %d", row); 
    } 
} 

Drucke:

2016-11-11 12:09:16.927 TEST[7126:382810] data at index: 0 
2016-11-11 12:09:16.927 TEST[7126:382810] data at index: 1 
2016-11-11 12:09:16.927 TEST[7126:382810] data at index: 2 
2016-11-11 12:09:16.928 TEST[7126:382810] Ad 
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 3 
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 4 
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 5 
2016-11-11 12:09:16.928 TEST[7126:382810] Ad 
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 6 
... 
+0

kein Glück die Anzeige immer noch das Objekt versteckt – sinfils

+0

Aktualisieren Sie Ihren Code in der Lage sein zu überprüfen. – shallowThought

+0

Bitte beachten Sie das Update – sinfils

0

Sie müssen die indexpath.row anpassen, um die übersprungenen Zellen widerzuspiegeln. Ersetzen Sie in Ihrem else-Teil indexPath.row mit recordedRow-Nummer.

else 
{ 

static NSString *simpleTableIdentifier = @"ArticleCell"; 
ArticleViewCell *cell = (ArticleViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
if ((cell == nil) || (![cell isKindOfClass: ArticleViewCell.class])) 
{ 
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticleCell" owner:self options:nil]; 
cell = [nib objectAtIndex:1]; 
cell = [[ArticleViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:simpleTableIdentifier] ; 
} 

NSInteger adjustedRow = indexPath.row - indexPath.row/4; 
NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:adjustedRow]; 

NSString *imageUrl = [[self.articlesArray objectAtIndex:adjustedRow]objectForKey:@"featured_image_thumbnail_tab_url"]; 
imageUrl = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
[cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl ] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
if (image){ 
    // Set your image over here 
}else{ 
    //something went wrong 
    NSLog(@"Error occured : %@", [error description]); 
} 
}]; 

NSString * title=[tempDictionary valueForKeyPath:@"title.rendered"]; 

cell.titleLabel.text = title; 


NSString * excerpt=[tempDictionary valueForKeyPath:@"excerpt.rendered"]; 
NSString *excerpt_rend =[self convertHTML:excerpt]; 
cell.excerptLabel.text = excerpt_rend; 

return cell; 
} 
0

Für Sie Beispiel-Array mit 8 Reihen haben und Sie möchten auch 2 Anzeigen, bedeutet insgesamt 10 Reihen. Aber Sie übergeben nur Array-Anzahl, die ein Problem verursacht. Um diese bitte folgende Code zu lösen

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section==0) 
    { 
     return 0; 
    } 
    else 
    { 

     return [self.articlesArray count] + ([self.articlesArray count]/4); 
    } 
} 

Fetch Daten aus dem Array, wie unten angegeben, wird es auf jeden Fall hilft Ihnen

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     //Your current code 
     int index = indexPath.row/4 
     NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:indexPath.row-index]; 

     //Your current code 
} 
+0

Ich bekomme eine NSRangeException als Index über Grenzen hinaus – sinfils

Verwandte Themen