user2402616
user2402616

Reputation: 1563

UITableView scrolling boundaries

I'm populating data into an array which is then being inserted into a UITableView. However once the screen gets filled, new cells won't show up because they get appended to the bottom. Is there a way to have it auto-scroll to the bottom upon adding an item? Also, the scrolling works but it isn't ideal. For example, to get to the last item, the user has to scroll and hold down that gesture to make the item appear. IE. My UITableView can hold up to 17 items in its view. After 17 the user can then scroll, however they would not be able to select item 18 because they would have to hold down their scroll gesture. In order to select item 18 they'd have to add say another 10 items.

- (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");
}

Upvotes: 0

Views: 376

Answers (3)

Ethan Fang
Ethan Fang

Reputation: 1269

You can not show the last item of the table.

The problem most likely is that your tableview’s height is bigger than you screen size. Or it can be auto layout issue. Try to make your tableview's height smaller.

Is there a way to have it auto-scroll to the bottom upon adding an item?

[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]

The following is my whole test Controller

@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

Upvotes: 1

simalone
simalone

Reputation: 2768

If you have to hold your drag gesture to show 18rd item, I think the problem is your tableView's height didn't set properly. Try to set height to your visible frame height.

Upvotes: 1

mengxiangjian
mengxiangjian

Reputation: 562

When the tableView was loaded,you can set contentOffset manually because you can get the tableViewContentSize this time.

Upvotes: 1

Related Questions