iOS UITableView点击按钮滚到顶部

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController = [[RootViewController alloc] init];

    [self.window makeKeyAndVisible];
    return YES;
}

@end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *datas;
}

@end

@implementation RootViewController

- (void)loadView{
    [super loadView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];

    [self initializeButtonWithFrame:CGRectMake(0, 0,100, 64) title:@"滚到顶部" action:@selector(scrollToTop:)];
    [self initializeButtonWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 0, 100, 60) title:@"滚到底部" action:@selector(scrollToButtom:)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"测试";

    datas = [[NSMutableArray alloc] init];
    for (int i = 0; i < 30; i++) {
        NSString *str = [NSString stringWithFormat:@"row:%d",i];
        [datas addObject:str];
    }

}
/**
 *  初始化按钮
 *
 *  @param frame 尺寸
 *  @param title 标题
 *  @param aSEL  按钮的方法
 */
- (void)initializeButtonWithFrame:(CGRect)frame title:(NSString*)title action:(SEL)aSEL{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor grayColor];
    btn.frame = frame;
    [btn setTitle:title forState:0];
    [btn addTarget:self action:aSEL forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor grayColor];
    [self.view addSubview:btn];
}

- (void)scrollToTop:(UIButton*)sender{
    NSLog(@"滚到顶部");
    NSIndexPath *topRow = [NSIndexPath indexPathForRow:0 inSection:0];
    [_tableView scrollToRowAtIndexPath:topRow atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)scrollToButtom:(UIButton*)sender{
    NSLog(@"滚到底部");
    NSIndexPath *buttomRow = [NSIndexPath indexPathForRow:datas.count - 1 inSection:0];
    [_tableView scrollToRowAtIndexPath:buttomRow atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark -- tableView的代理 --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return datas.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = datas[indexPath.row];
    return cell;
}

@end
时间: 2024-08-24 18:41:29

iOS UITableView点击按钮滚到顶部的相关文章

Web前端 页面功能——点击按钮返回顶部的实现方法

1. 最简单的静态返回顶部,点击直接跳转页面顶部,常见于固定放置在页面底部返回顶部功能 方法一:用命名锚点击返回到顶部预设的id为top的元素 html代码 <a href="#top" target="_self">返回顶部</a> 方法二:操作scrooll函数用来控制滚动条的位置(第一个参数是水平位置,第二个参数是垂直位置) html代码 <a href="javascript:scroll(0,0)">

iOS UITableView中点击状态栏无法回滚到顶部

// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, an

点击按钮回到顶部

写页面的时候,经常遇到当滚动条离开页面顶部的时候,会显示一个回到顶部的按钮,点击回到顶部,页面就滑动到页面的顶部.直接贴代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>返回顶部</title> <script src="jquery-1.11.3.js"></script> <style&

小程序-点击按钮回到顶部1

以下代码的缺点:点击顶部按钮是直接回到顶部,而不是均匀的滑动至顶部 <scroll-view class="bigWrap" scroll-y="true" scroll-top="{{scrollTop}}" bindscroll="scroll" style="position: absolute; left: 0; top:0; bottom: 0; right: 0;"> <vie

IOS UITableView 底部添加按钮

一 要求效果 实现效果,在UITableView最底部添加提交按钮. 可以通过UITableView tableFooterView 实现 // accessory view below content. default is nil. not to be confused with section footer @property (nonatomic, retain) UIView *tableFooterView; 二 代码实现 - (void)viewDidLoad {     [sup

Vue中点击按钮回到顶部(滚动效果)

页面滚动到一定位置时,出现回到顶部按钮 代码如下 HTML <div class="footer"> <div class="gotop" v-show="gotop" @click="toTop">Top</div> </div> CSS .footer .gotop { text-align: center; position: fixed; right: 50px; bot

iOS开发——实战技术OC篇&amp;点击状态栏ScrollView(包括子控件)自动滚到顶部

点击状态栏ScrollView(包括子控件)自动滚到顶部 其实这种方式我们平时见的还是比较多的,而且适合用户的需求,所以就搬来琢磨了一下,感觉效果还不错 这里就直接将解决思路一一写出来不将代码分段展示了,在代码中我加了详细的注释objective-c的套路和swift基本一样,在最后会将Swift和objective-c的代码一起放上,如果需要直接解决问题的童鞋可以直接将代码拷贝到工程里即可 首先创建一个topWindow继承至NSObject,这里我们考虑将这个功能完全封装起来,所以所有的方法

iOS开发——实用技术OC片&amp;点击状态栏回到顶部

点击状态栏回到顶部 经常我们在使用一个App的时候,比如QQ,微信等流行App都会有一个很常见的功能,就是当我们刷新了很多节目的时候,先立刻回到顶部只需要轻轻点一下状态栏就可以(当然这种方法不是谁都知道的,因为app没有提示),也有的会在屏幕的右下角或者某个位置放置一个按钮实现点击按钮一样可以回到顶部,那样实现虽然可以,也不是很麻烦,但是其实系统已经为我们提供了一个很好的自带的功能,我们为什么不用呢? 但是最近在自己视线这个功能的时候遇到了一个小小的问题,那就拿出来分享一下,当然前面也有说到类似

点击按钮,回到页面顶部的5种写法

1.锚点方式: 1 <body style="height:2000px;"> 2 <div id="topAnchor"></div> 3 <a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到顶部</a> 4 </body> 2.scrollTop:scrollTop属性表示被隐藏