드롭다운 새로 고침 및 위로 당기기 추가
5429 단어 iosiPhone드롭다운 새로 고침추가 로드
여기는 핵심 기능의 코드만 열거하고, 다른 코드는 첨부파일을 참고하십시오.
DemoTableViewController.h
#import <UIKit/UIKit.h>
#import "STableViewController.h"
@interface DemoTableViewController : STableViewController {
NSMutableArray *items;
}
@end
DemoTableViewController.m
#import "DemoTableViewController.h"
#import "DemoTableHeaderView.h"
#import "DemoTableFooterView.h"
@interface DemoTableViewController ()
- (void) addItemsOnTop;
- (void) addItemsOnBottom;
- (NSString *) createRandomValue;
@end
@implementation DemoTableViewController
- (void) viewDidLoad {
[super viewDidLoad];
self.title = @"STableViewController Demo";
[self.tableView setBackgroundColor:[UIColor lightGrayColor]];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DemoTableHeaderView" owner:self options:nil];
DemoTableHeaderView *headerView = (DemoTableHeaderView *)[nib objectAtIndex:0];
self.headerView = headerView;
nib = [[NSBundle mainBundle] loadNibNamed:@"DemoTableFooterView" owner:self options:nil];
DemoTableFooterView *footerView = (DemoTableFooterView *)[nib objectAtIndex:0];
self.footerView = footerView;
items = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++)
[items addObject:[self createRandomValue]];
}
#pragma mark - Pull to Refresh
- (void) pinHeaderView {
[super pinHeaderView];
DemoTableHeaderView *hv = (DemoTableHeaderView *)self.headerView;
[hv.activityIndicator startAnimating];
hv.title.text = @"Loading...";
}
- (void) unpinHeaderView {
[super unpinHeaderView];
[[(DemoTableHeaderView *)self.headerView activityIndicator] stopAnimating];
}
- (void) headerViewDidScroll:(BOOL)willRefreshOnRelease scrollView:(UIScrollView *)scrollView {
DemoTableHeaderView *hv = (DemoTableHeaderView *)self.headerView;
if (willRefreshOnRelease)
hv.title.text = @"Release to refresh...";
else
hv.title.text = @"Pull down to refresh...";
}
- (BOOL) refresh {
if (![super refresh])
return NO;
[self performSelector:@selector(addItemsOnTop) withObject:nil afterDelay:2.0];
return YES;
}
#pragma mark - Load More
- (void) willBeginLoadingMore {
DemoTableFooterView *fv = (DemoTableFooterView *)self.footerView;
[fv.activityIndicator startAnimating];
}
- (void) loadMoreCompleted {
[super loadMoreCompleted];
DemoTableFooterView *fv = (DemoTableFooterView *)self.footerView;
[fv.activityIndicator stopAnimating];
if (!self.canLoadMore) {
fv.infoLabel.hidden = NO;
}
}
- (BOOL) loadMore {
if (![super loadMore])
return NO;
[self performSelector:@selector(addItemsOnBottom) withObject:nil afterDelay:2.0];
return YES;
}
#pragma mark - Dummy data methods
- (void) addItemsOnTop {
for (int i = 0; i < 3; i++)
[items insertObject:[self createRandomValue] atIndex:0];
[self.tableView reloadData];
[self refreshCompleted];
}
- (void) addItemsOnBottom {
for (int i = 0; i < 5; i++)
[items addObject:[self createRandomValue]];
[self.tableView reloadData];
if (items.count > 50)
self.canLoadMore = NO;
else
self.canLoadMore = YES;
[self loadMoreCompleted];
}
- (NSString *) createRandomValue {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
return [NSString stringWithFormat:@"%@ %@", [dateFormatter stringFromDate:[NSDate date]],
[NSNumber numberWithInt:rand()]];
}
#pragma mark - Standard TableView delegates
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return items.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [items objectAtIndex:indexPath.row];
return cell;
}
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.