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;
      
     

    }

ios objective-c uitableview

初音de猫 9 years, 2 months ago

Your Answer