[课堂实践与项目]使用ARC下UITableView实现上拉、下拉刷新功能。

这篇博客介绍了如何在采用ARC(Automatic Reference Counting)的Xcode项目中,利用UITableView实现上拉、下拉刷新功能。内容涉及到将UITableView替换为PullTableView,并详细说明了在ARC和非ARC混合模式下,如何通过添加编译标志来调整代码的内存管理行为。操作步骤包括在Build Phases中修改Compile Sources的设置,为相应文件添加-fobjc-arc或-fno-objc-arc标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在显示VC的h文件中写入如下的代码

#import <UIKit/UIKit.h>
#import "PullTableView.h"

@interface LCViewController : UIViewController <UITableViewDataSource, PullTableViewDelegate>
{
    PullTableView *pullTableView;
}
@property (nonatomic, retain) IBOutlet PullTableView *pullTableView;

@end

2.在实现文件中实现如下的代码

//
//  LCViewController.m
//  上下拉刷新
//
//  Created by lichan on 13-12-16.
//  Copyright (c) 2013年 lichan. All rights reserved.
//

#import "LCViewController.h"



@interface LCViewController ()
@end

@implementation LCViewController
@synthesize pullTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pullTableView.pullArrowImage = [UIImage imageNamed:@"blackArrow"];
    self.pullTableView.pullBackgroundColor = [UIColor yellowColor];
    self.pullTableView.pullTextColor = [UIColor blackColor];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    //如果没有刷新,则进行刷新操作
    if(!self.pullTableView.pullTableIsRefreshing) {
        self.pullTableView.pullTableIsRefreshing = YES;
        [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
        
        
    }
}

#pragma mark - Refresh and load more methods

- (void) refreshTable
{
    /*
     
     Code to actually refresh goes here.
     
     */
    self.pullTableView.pullLastRefreshDate = [NSDate date];
    self.pullTableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
    /*
     
     Code to actually load more data goes here.
     
     */
    self.pullTableView.pullTableIsLoadingMore = NO;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Row %i", indexPath.row];
    
    return cell;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Section %i begins here!", section];
}

- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Section %i ends here!", section];
    
}

#pragma mark - PullTableViewDelegate

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3.一定要注意的是,我们的tableView继承的类是

PullTableView  而非我们原来的UItableVIew。

4.如果使用的是ARC,则需要相应的变换。


Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式。

如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签。

如果你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标签。

添加标签的方法:

  1. 打开:你的target -> Build Phases -> Compile Sources.

  2. 双击对应的 *.m 文件

  3. 在弹出窗口中输入上面提到的标签 -fobjc-arc / -fno-objc-arc

  4. 点击 done 保存


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值