UIScrollView,UITableView的简单使用
程序员文章站
2022-06-01 11:18:28
...
UIScrollView的简单使用
1,创建对象
2,设置滑动区域
3,创建滑动的View
4,将滑动的view添加到ScrollView上显示
5,释放对象
CGRect screenBounds = [ [UIScreen mainScreen]bounds];//返回的是带有状态栏的Rect
// CGRect rect = [ [UIScreen mainScreen]application Frame];//不包含状态栏的Rect
//创建ScrollViewiew
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 500.0f, 500.0f)];
scrollView.frame=screenBounds;
scrollView.backgroundColor=[UIColor redColor];
//设置ScollView的滑动区域
scrollView.contentSize=CGSizeMake(500, 1100);
UIImageView *myImageView= [[UIImageView alloc] initWithFrame: CGRectMake( 0, 0, 500, 1100)];
[myImageView setImage:[UIImage imageNamed:@"bk.jpg"]];
//img添加到Scollview
[scrollView addSubview:myImageView];
//scollView添加到View
[self.view addSubview:scrollView];
[scrollView release];
[myImageView release];
2,UITableView的简单使用,由于最近在做金融相关的开发大量使用到了table相关的控件,android的控件都要自己实现,ios确实自带的效果,想想就有点操蛋 ,,,谁叫ios比android要简单呢?
1,定义标题数据
2,定义标题分别对应的数据
3,实现数据源代理和表格代理(UITableViewDataSource,UITableViewDelegate)
4,创建TableView
5,实现相关的方法
#import "TableViewController.h"
@interface TableViewController (){
UITableView *table;
}
@property(strong,nonatomic)NSMutableArray *arrayList1; //数据源
@property (strong,nonatomic)NSMutableArray *arrayTitle;//标题
@property(strong,nonatomic)NSMutableArray *arrayList2;
@end
@implementation TableViewController
@synthesize arrayList1;
@synthesize arrayTitle;
@synthesize arrayList2;
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 430, 600)];
//
table.dataSource=self;
table.delegate=self;
if (arrayList1==nil) {
arrayList1 =[[NSMutableArray alloc]initWithObjects:@"美元/日元",@"美元/韩元",@"韩元/日元",@"港币/日元",@"美元/欧元",@"美元/英镑",@"美元/新加坡元",@"美元/澳元",@"美元/卢布",@"泰铢/日元",@"英镑/泰铢",@"新加坡元/澳门元",@"美元/港币",@"港币/台币",@"台币/越南盾",@"美元/韩元", nil];
arrayTitle=[[NSMutableArray alloc]initWithObjects:@"自选",@"交叉", nil];
arrayList2=[[NSMutableArray alloc]initWithObjects:@"美元/港币",@"韩元/日元", nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//设置标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [arrayTitle objectAtIndex:section];
}
#pragma mark - Table view data source
//指定多少分区,根据arrayTitle计算
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return [arrayTitle count];
}
//设置数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
//1,根据当前点击的区域设置数据
switch (section) {
case 0:
return [arrayList1 count];
case 1:
return [arrayList2 count];
default:
break;
}
return 0;
}
//绘制数据 cellForRowAtINdexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
static NSString * string = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
//根据分区操作
switch (indexPath.section) {
case 0:
[[cell textLabel] setText:[arrayList1 objectAtIndex:indexPath.row]];
break;
case 1:
[[cell textLabel] setText:[arrayList2 objectAtIndex:indexPath.row]];
break;
default:
break;
}
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
// Override to support conditional rearranging of the
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
@end
1,titleForHeaderInSection 设置数据
2,numberOfSectionsInTableView 根据标题数目进行分区
3,numberOfRowsInSection 指定区域数据的条数
4,cellForRowAtIndexPath 绘制数据、
创建cell
static NSString * string = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
上一篇: 雷军再开微博发布会 小米9升级新功能:三摄平滑切换
下一篇: iOS之UITableView