tableview自动滑动的奇怪现象

创建一个tableview,让他在view出现后开始滑动,如果设置的偏移量大于这个tableview的高度时,会出现滑动断层的现象。

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong , nonatomic) UITableView * tableView;

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor redColor];
    
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(100, 200, 200, 300)style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    self.tableView = tableView;
    
    [self.view addSubview:self.tableView];

    }

  • (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:5.0f animations:^{
        [self.tableView setContentOffset:CGPointMake(0, 300) animated:NO];
        //        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:19 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }];
    

    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return 20;

    }

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

    
    static NSString * identifier = @"identifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"  %ldcell",indexPath.row];
    NSLog(@"*******************%ld cell",indexPath.row);
    return cell;

    }

阅读 6.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题