My UITableView won't scroll down through the e

2019-02-08 06:11发布

Ok I don't know why this isn't working, but I have hooked up a tableView, with 19 items of text I'd like to set to each cell.

The cells populate just fine, but when I try and scroll, it goes down there and I can see the cells that aren't visible on the initial view, it hangs, and won't scroll down. It just snaps back. REALLY WEIRD!

What am I doing wrong?

- (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.
return 19;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
if(indexPath.row == 0){
    cell.textLabel.text = @"";
}else if(indexPath.row == 1){
    cell.textLabel.text = @"";
}else if(indexPath.row == 2){
    cell.textLabel.text = @"";
}else if(indexPath.row == 3){
    cell.textLabel.text = @"";
}else if(indexPath.row == 4){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 5){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 6){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 7){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 8){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 9){
    cell.textLabel.text = @"";
}else if(indexPath.row == 10){
    cell.textLabel.text = @"";
}else if(indexPath.row == 11){
    cell.textLabel.text = @"";
}else if(indexPath.row == 12){
    cell.textLabel.text = @"";
}else if(indexPath.row == 13){
    cell.textLabel.text = @"";
}else if(indexPath.row == 14){
    cell.textLabel.text = @"";
}else if(indexPath.row == 15){
    cell.textLabel.text = @"";
}else if(indexPath.row == 16){
    cell.textLabel.text = @"";
}else if(indexPath.row == 17){
    cell.textLabel.text = @"";
}else if(indexPath.row == 18){
    cell.textLabel.text = @"";
}

return cell;
}

8条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-08 06:39

You need to either adjust the height of you UITableView or you can try to put [tableView reloadData] inside viewDidAppear method (it doesn't seem to work if put inside viewWillAppear or viewDidLoad though).

The former works for me in most cases.

查看更多
做自己的国王
3楼-- · 2019-02-08 06:42

The code that you shared looks fine to me. Are you pushing view controller with this tableview on navigation controller stack ? I have seen people do that before. Default height of cell is 44px and height of navigation bar is 44px as well. So, if you push this on navigation controller stack you scroll view snaps back like you have described. If that is the case reduce height of TableView by 44 px ( in Interface builder or Programatically if you drew this programatically) and it should fix it.

查看更多
\"骚年 ilove
4楼-- · 2019-02-08 06:43

If you have a storyboard or nib try this (note, you might have to add some constraints if you want to support landscape).

enter image description here

查看更多
你好瞎i
5楼-- · 2019-02-08 06:47

@EmptyStack answer is right.That was a alternate but NOT the Solution.

Even we can achieve the same in storyboard.

Select tableView --> from the storyboard control click on 'add Missing constraints'. That will add the constraint to tableView and View.

That helped me to resolve this issue. A screen_shot attached for reference.solution

查看更多
虎瘦雄心在
6楼-- · 2019-02-08 06:50

Decrease your tableView's height.

查看更多
Rolldiameter
7楼-- · 2019-02-08 06:54

This helped me:

[self.tableView setContentInset:UIEdgeInsetsMake(0,0,65,0)];
查看更多
登录 后发表回答