0

我有一个非常小而烦人的问题。(没有双关语)我似乎发现了我的错误。我也在聊天中问过,但我们找不到解决方案。我从 Abizerns github 获得了我的 tableView 的“加载更多...”并实现了它。它可以工作,除了部分,当它启动时我必须单击“加载更多...”。所以在开始时它不会向我显示 tableView 的第一个条目,直到我单击“加载更多...”。你可以看到我的烦恼。也许有人会发现我的错误,因此 tableView 实际上会在启动时加载前十个条目。

#define kNumberOfItemsToAdd 10
#define PADDING 10.0f

@interface Main ()

@end

@implementation Main
@synthesize tabelle;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    numberOfItemsToDisplay = kNumberOfItemsToAdd;
    return self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (numberOfItemsToDisplay == [tabelle count]) {
        return 1;
    }
    return 2;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 6;
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.numberOfLines = 1;
    }

    if (indexPath.section == 0) {            

    cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:14.0];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Verdana" size:12.0];

    NSString *TableText = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] Name]];
    NSString *TableText2 = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] m_m]];  

    NSString *cellValue = [NSString stringWithFormat:@"%@", TableText2];
    NSString *cellValue2 = [NSString stringWithFormat:@"by: %@", TableText];
    cell.textLabel.text = cellValue;
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
    cell.detailTextLabel.text = cellValue2;
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"Next %d items", kNumberOfItemsToAdd];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14.f];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [[self.tabelle objectAtIndex:indexPath.row] m_m];
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];

    return textSize.height + PADDING * 3;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        NSUInteger i, totalNumberOfItems = [tabelle count];        
        NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];        

        for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++) {
            [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }        

        numberOfItemsToDisplay = newNumberOfItemsToDisplay;                

        [tableView beginUpdates];
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
        if (numberOfItemsToDisplay == totalNumberOfItems) {
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
        }        
        [tableView endUpdates];
        // Scroll the cell to the top of the table
        if (newNumberOfItemsToDisplay < totalNumberOfItems) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
                [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
            });
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
                [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:totalNumberOfItems-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            });
        }

    }    

}
4

2 回答 2

1

我没有使用“加载更多... ”实现,但也许您需要[self.tabelle reloadData]在您的- (void)viewWillAppear:(BOOL)animated方法中添加。

于 2012-05-09T19:46:45.663 回答
1

单击“加载更多...”时会调用什么方法?加载表格视图时,您不能以编程方式调用该方法吗?

于 2012-05-09T20:15:12.170 回答