2013-01-05 107 views
1

试图让它加载更多的细胞,但我不能让它出现在其他细胞后,遗憾的是模糊的,但生病你展示我的代码...表视图加载更多单元格?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [rows count] + 1; 
} 

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

    static NSString *postCellId = @"postCell"; 
    UITableViewCell *cell = nil; 

    cell = [self.tableView dequeueReusableCellWithIdentifier:postCellId]; 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId]; 

    NSUInteger row = [indexPath row]; 
    NSUInteger count = [rows count]; 

    if (row == count) { 
     loadMore = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 320, 25)]; 
     loadMore.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
     loadMore.font = [UIFont fontWithName:@"AvenirNext-Bold" size:18.0]; 
     loadMore.textColor = [UIColor colorWithRed:60.0/255.0 green:58.0/255.0 blue:55.0/255.0 alpha:1.0]; 
     loadMore.text = @"Load More..."; 
     loadMore.textAlignment = UITextAlignmentCenter; 
     [cell.contentView addSubview:loadMore]; 


     displaying = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 25)]; 
     displaying.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
     displaying.font = [UIFont fontWithName:@"AvenirNext-Bold" size:12.0]; 
     displaying.textColor = [UIColor colorWithRed: 115.0/255.0 green: 113.0/ 255.0 blue:112.0/255.0 alpha:1.0]; 
     displaying.text = [NSString stringWithFormat:@"Displaying %i Results", rows.count]; 
     displaying.textAlignment = UITextAlignmentCenter; 
     [cell.contentView addSubview:displaying]; 

    } else { 

    dict = rows[indexPath.row]; 

    thumbimgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)]; 
    businessLabel = [[UILabel alloc] initWithFrame:CGRectMake(103, 10, 207, 25)]; 
    locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(103, 35, 207, 35)]; 
    locimgView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 75, 20, 15)]; 
    distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 75, 60, 15)]; 

    businessLabel.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
    businessLabel.font = [UIFont fontWithName:@"AvenirNext-Bold" size:18.0]; 
    businessLabel.textColor = [UIColor colorWithRed:60.0/255.0 green:58.0/255.0 blue:55.0/255.0 alpha:1.0]; 
    businessLabel.text = dict[@"business"]; 
    [cell.contentView addSubview:businessLabel]; 

    locationLabel.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
    locationLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:14.0]; 
    locationLabel.text = dict[@"location"]; 
    locationLabel.textColor = [UIColor colorWithRed: 115.0/255.0 green: 113.0/ 255.0 blue:112.0/255.0 alpha:1.0]; 
    locationLabel.numberOfLines = 2; 
    locationLabel.lineBreakMode = UILineBreakModeTailTruncation; 

    [cell.contentView addSubview:locationLabel]; 

    listingLoc = [[CLLocation alloc] initWithLatitude:[dict[@"lat"] doubleValue] longitude:[dict[@"lon"] doubleValue]]; 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    float kilometers = [appDelegate.currentLoc distanceFromLocation:listingLoc]/1000; 

    int milesint = kilometers * 0.621371192; 

    NSString *milesOut = [NSString stringWithFormat:@"%i miles", milesint]; 

    distanceLabel.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
    distanceLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:12.0]; 
    distanceLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
    distanceLabel.text = milesOut; 
    [cell.contentView addSubview:distanceLabel]; 

    NSArray *splitImages = [dict[@"image1"] componentsSeparatedByString:@", "]; 

    icon = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[splitImages objectAtIndex:0]]]]; 

    [thumbimgView setContentMode:UIViewContentModeScaleAspectFill]; 
    [thumbimgView setClipsToBounds:YES]; 

    thumbimgView.image = icon; 
    [cell.contentView addSubview:thumbimgView]; 

    locimgView.image = [UIImage imageNamed:@"location-icon.png"]; 
    [cell.contentView addSubview:locimgView]; 

    cell.textLabel.text = dict[@"business"]; 
    cell.textLabel.hidden = YES; 

    } 

    return cell; 
} 

它所做的就是返回数据从数组中,并没有细胞后加载更多..非常感谢。

+0

行声明在哪里? – Andy

+0

你可以从didselect行发布一些代码吗? –

+0

更新后的代码。 – rjg

回答

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

    NSUInteger row = [indexPath row]; 
    NSUInteger count = [rows count]; 
    UITableViewCell *cell = nil;  

    if (row == count) { 
     // This is the bottom most cell, therefore display the More-Cell 
     static NSString *postCellId = @"moreCell"; 

     cell = [self.tableView dequeueReusableCellWithIdentifier:postCellId]; 
     if (cell == nil) { // it cannot be re-used, so create a new one 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId]; 
     // now add all sub views such as labels and buttons. 
     // If the layout is always the same then you may set it here. 

     } 
     // Now it is either re-used or newly created. However, it carries all the subviews. 
     // Here set the values. You may set the layout here too, if the layout depends on data. 

    } else { 
     // Display a regular Post-Cell  
     static NSString *postCellId = @"postCell"; 

     cell = [self.tableView dequeueReusableCellWithIdentifier:postCellId]; 
     if (cell == nil) { // it cannot be re-used, so create a new one 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId]; 
     // now add all sub views such as labels and buttons. 
     // If the layout is always the same then you may set it here. 

     } 
     // Now it is either re-used or newly created. However, it carries all the subviews. 
     // Here set the values. You may set the layout here too, if the layout depends on data. 

    } 

    return cell; 
} 
+0

感谢Hermann,用代码试过了,它仍然没有返回底部加载更多单元格,只剩下其余部分,我想出于某种原因,额外的单元格没有被添加到显示数据,indexPath。当我把这个'NSLog(@“%i”,indexPath.row);'它然后在3日志:'0 0 1'如果你有任何线索? – rjg

+0

@RyanGittings,请继续并提供更多的调试数据。编辑你的问题。NSLog计数在numberOfRowsInSection中,当前行在cellForRow ...和NSLog中再次计数cellForRow ... so我们看到[rows count]是否在此期间发生了变化。将NSLog语句添加到问题中的代码中,并提供输出在对你的问题进行编辑的过程中,让每个人清楚地看到发生了什么。 –

+0

全部排序,您的代码帮助。非常感谢! – rjg

0

我没有看到你实施这个方法的问题。我简化了代码(主要在“else”子句中)并创建了一个虚拟数组,并且一切正常。请注意,我正在使用新的范例来创建表格视图单元格,因此不需要“if cell is nil”语句。

@implementation TableController { 
    NSArray *rows; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"postCell"]; 
    rows = @[@"One",@"Two",@"Three",@"Four"]; 

} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [rows count] + 1; 
} 

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"postCell" forIndexPath:indexPath]; 

    NSUInteger row = [indexPath row]; 
    NSUInteger count = [rows count]; 

    if (row == count) { 
     UILabel *loadMore = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 320, 25)]; 
     loadMore.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
     loadMore.font = [UIFont fontWithName:@"AvenirNext-Bold" size:18.0]; 
     loadMore.textColor = [UIColor colorWithRed:60.0/255.0 green:58.0/255.0 blue:55.0/255.0 alpha:1.0]; 
     loadMore.text = @"Load More..."; 
     loadMore.textAlignment = NSTextAlignmentCenter; 
     [cell.contentView addSubview:loadMore]; 


     UILabel *displaying = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 25)]; 
     displaying.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
     displaying.font = [UIFont fontWithName:@"AvenirNext-Bold" size:12.0]; 
     displaying.textColor = [UIColor colorWithRed: 115.0/255.0 green: 113.0/ 255.0 blue:112.0/255.0 alpha:1.0]; 
     displaying.text = [NSString stringWithFormat:@"Displaying %i Results", rows.count]; 
     displaying.textAlignment = NSTextAlignmentCenter; 
     [cell.contentView addSubview:displaying]; 
    } else { 
     cell.textLabel.text = rows[indexPath.row]; 
    } 
    return cell; 
} 
相关问题