UITATIONVIEW滚动边界



我将数据填充到一个数组中,然后将数据插入UITableView。但是,一旦屏幕填充,新的单元格就不会出现,因为它们会附加到底部。是否有一种方法可以在添加项目后将其自动滚动到底部?另外,滚动有效,但并不理想。例如,要到达最后一项,用户必须滚动并按住该手势才能使该项目出现。IE。我的UITableView在其视图中最多可以容纳17个项目。17年后,用户可以滚动,但是他们将无法选择项目18,因为他们必须按住滚动手势。为了选择项目18,他们必须添加另外10个项目。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    // Usually the number of items in your array (the one that holds your list)
    return [_items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Where we configure the cell in each row
    static NSString *CellIdentifier = @"trap";
    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell... setting the text of our cell's label
    cell.textLabel.text = [_items objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"test");
}

如果您必须保持阻力手势以显示第18个项目,我认为问题是您的TableView的高度无法正确设置。尝试将高度设置为可见的框架高度。

加载tableview时,您可以手动设置ContentOffset,因为您可以在此时间获得tableViewContentsize。

您无法显示表的最后一项。

问题很可能是您的TableView的高度大于 您的屏幕尺寸。或者可能是自动布局问题。尝试使您的餐桌的高度较小。

添加项目时是否有一种方法可以将其自动滚动到底部?

[self.items addObject:[NSString stringWithFormat:@"Trawl %d", count]];
[self.tableView reloadData];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.item.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]

以下是我的整个测试控制器

@interface DPCalendarTestCreateEventViewController ()<UITableViewDelegate, UITableViewDataSource, DPCalendarTestOptionsCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *items;
@end
@implementation DPCalendarTestCreateEventViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.items = @[@"TEST", @"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST",@"TEST"].mutableCopy;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonSelected)];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.tableView];
}
- (void) doneButtonSelected{
    [self.items addObject:[NSString stringWithFormat:@"Trawl %d", self.items.count]];
    [self.tableView reloadData];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Where we configure the cell in each row
    static NSString *CellIdentifier = @"trap";
    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell... setting the text of our cell's label
    cell.textLabel.text = [_items objectAtIndex:indexPath.row];
    return cell;
}
@end

最新更新