使用 UITableView 布局 类似随手记的账户界面(实体机运行效果)
代码如下:
头文件:
#import <UIKit/UIKit.h>
@interface TextTabViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UITableView *uiTableView;
@property(nonatomic,strong) NSMutableArray* arrayTypeName;//保存页眉名称
@property(nonatomic,strong) NSMutableArray* arrayTypeMoney;//保存页眉右侧金额
@property(nonatomic,strong) UIImage *image;//悲剧图片
@property(nonatomic,assign) CGRect windowRect;//保存窗体大小
@property(nonatomic,strong) NSString* strY;//金钱符号
@property(nonatomic,strong) NSMutableDictionary* mutableDictionary;//保存row标题;
@end
M文件:
#import "TextTabViewController.h"
@interface TextTabViewController ()
@end
@implementation TextTabViewController
@synthesize uiTableView;
@synthesize arrayTypeMoney;
@synthesize arrayTypeName;
@synthesize windowRect;
@synthesize strY;
@synthesize mutableDictionary;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
strY=@"¥";
self.uiTableView=[[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
//初始化数组
arrayTypeName=[[NSMutableArrayalloc]init];
[arrayTypeNameaddObject:@"现金账户"];
[arrayTypeNameaddObject:@"金融账户"];
[arrayTypeNameaddObject:@"虚拟账户"];
[arrayTypeNameaddObject:@"信用卡账户"];
[arrayTypeNameaddObject:@"负债账户"];
[arrayTypeNameaddObject:@"债权账户"];
arrayTypeMoney=[[NSMutableArrayalloc]init];
[arrayTypeMoneyaddObject:@"-120.00"];
[arrayTypeMoneyaddObject:@"0.00"];
[arrayTypeMoneyaddObject:@"0.00"];
[arrayTypeMoneyaddObject:@"0.00"];
[arrayTypeMoneyaddObject:@"0.00"];
[arrayTypeMoneyaddObject:@"0.00"];
//设置悲剧image
self.image=[UIImageimageNamed:@"2.png"];
UIImageView *imageView=[[UIImageViewalloc]initWithImage:self.image];
imageView.frame=CGRectMake(0.0f,0.0f,10.0f,50.0f);
//设置左边lable
UILabel *labelLeft=[[UILabelalloc]initWithFrame:CGRectMake(10.0f,22.0f,60.0,20.0)];
labelLeft.font=[UIFontboldSystemFontOfSize:16];
labelLeft.text=@"净资产:";
labelLeft.backgroundColor=[UIColorclearColor];
[imageViewaddSubview:labelLeft];
//设置右边lable值
self.windowRect=self.view.bounds;
UILabel *labelRight=[[UILabelalloc]init];
labelRight.font=[UIFontboldSystemFontOfSize:16];
labelRight.text=@"-¥120.00";
labelRight.backgroundColor=[UIColorclearColor];
labelRight.frame=CGRectMake(windowRect.size.width-labelRight.text.length*10,22.0f, labelRight.text.length*10,20.0);
[imageViewaddSubview:labelRight];
mutableDictionary=[[NSMutableDictionaryalloc]init];
[mutableDictionarysetObject:[[NSMutableArrayalloc]initWithObjects:@"现金",@"其他",nil]forKey:@"0"];
[mutableDictionarysetObject:[[NSMutableArrayalloc]initWithObjects:@"存折",@"银行卡",nil]forKey:@"1"];
[mutableDictionarysetObject:[[NSMutableArrayalloc]initWithObjects:@"财付通",@"支付宝",@"饭卡",@"公交卡",nil] forKey:@"2"];
[mutableDictionarysetObject:[[NSMutableArrayalloc]initWithObjects:@"信用卡",nil]forKey:@"3"];
[mutableDictionarysetObject:[[NSMutableArrayalloc]initWithObjects:@"应付款项",nil]forKey:@"4"];
[mutableDictionarysetObject:[[NSMutableArrayalloc]initWithObjects:@"应收款项",nil]forKey:@"5"];
self.uiTableView.dataSource=self;
self.uiTableView.delegate=self;
self.uiTableView.autoresizesSubviews = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.uiTableViewsetTableHeaderView:imageView];
[self.viewaddSubview:self.uiTableView];
}
//设置Section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([tableViewisEqual:self.uiTableView])
{
returnarrayTypeName.count;
}
return0;
}
//设置每个section里面行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableViewisEqual:self.uiTableView])
{
NSString* strKey=[NSStringstringWithFormat:@"%d",section];
NSMutableArray *arr= [mutableDictionary objectForKey:strKey];
return arr.count;
}
return0;
}
//设置cell内容
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableViewisEqual:self.uiTableView])
{
UITableViewCell *tableViewCell=[self.uiTableView dequeueReusableHeaderFooterViewWithIdentifier:@"Cells"];
if(tableViewCell==nil)
{
tableViewCell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"Cells"];
}
//获取数组
NSString* strKey=[NSStringstringWithFormat:@"%d",indexPath.section];
NSMutableArray *arr= [mutableDictionary objectForKey:strKey];
//设置当前cells第一个lable值
UILabel *labelTop=[[UILabelalloc]init];
labelTop.font=[UIFontboldSystemFontOfSize:10.0f];
labelTop.text=arr[indexPath.row];
labelTop.backgroundColor=[UIColorclearColor];
labelTop.frame=CGRectMake(10,8.0f,80,10.0);
labelTop.textColor=[UIColorblackColor];
[tableViewCelladdSubview:labelTop];
//设置第二排文字
UILabel *labelDown=[[UILabelalloc]init];
labelDown.font=[UIFontsystemFontOfSize:8.0f];
labelDown.text=@"人民币 | 在线支付";
labelDown.backgroundColor=[UIColorclearColor];
labelDown.frame=CGRectMake(10,20.0f,80,15.0);
labelDown.textColor=[UIColorbrownColor];
[tableViewCelladdSubview:labelDown];
//设置右边金额
UILabel *labelRight=[[UILabelalloc]init];
labelRight.font=[UIFontboldSystemFontOfSize:10.0f];
labelRight.text=@"¥ 0.00";
labelRight.backgroundColor=[UIColorclearColor];
labelRight.frame=CGRectMake(windowRect.size.width-65,15.0f,50,10.0);
labelRight.textColor=[UIColorblackColor];
[tableViewCelladdSubview:labelRight];
//设置右边图标
tableViewCell.accessoryType =UITableViewCellSeparatorStyleSingleLine;
tableViewCell.selectionStyle=UITableViewCellSelectionStyleGray;
return tableViewCell;
}
return nil;
}
//设置Section的页眉
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if([tableViewisEqual:self.uiTableView])
{
//定义imageView
UIImageView *imageView=[[UIImageViewalloc]initWithImage:self.image];
imageView.frame=CGRectMake(0.0f,0.0f,10.0f,30.0f);
//定义lable用来显示左边的文字
UILabel *labelLeft=[[UILabelalloc]init];
labelLeft.font=[UIFontsystemFontOfSize:14.0f];
labelLeft.text=self.arrayTypeName[section];
labelLeft.backgroundColor=[UIColorclearColor];
labelLeft.textColor=[UIColorwhiteColor];
labelLeft.frame=CGRectMake(10.0f,8.0f,100,15.0);
[imageViewaddSubview:labelLeft];
//定义lable用来显示右边的文字
UILabel *labelRight=[[UILabelalloc]init];
labelRight.font=[UIFontsystemFontOfSize:14.0f];
labelRight.text=[strYstringByAppendingString:self.arrayTypeMoney[section]];
labelRight.backgroundColor=[UIColorclearColor];
labelRight.frame=CGRectMake(windowRect.size.width-80,8.0f,80,15.0);
labelRight.textColor=[UIColorwhiteColor];
//把label添加到 imageView中
[imageViewaddSubview:labelRight];
return imageView;
}
return nil;
}
//设置Section的页眉的行高
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return30.0f;
}
//设置section的rows行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return40.0f;
}
//滑动的时候 出现删除按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//滑动时候触发事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//一定要判断当前是否wie删除操作
if (editingStyle ==UITableViewCellEditingStyleDelete)
{
//获取数组
NSString* strKey=[NSStringstringWithFormat:@"%d",indexPath.section];
NSMutableArray *arr= [mutableDictionary objectForKey:strKey];
//移出数据
[arrremoveObjectAtIndex:indexPath.row];
[uiTableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
uiTableView=nil;
arrayTypeName=nil;//保存页眉名称
arrayTypeMoney=nil;//保存页眉右侧金额
self.image=nil;//悲剧图片
mutableDictionary=nil;//保存row标题;
}
滑动删除时候后的效果
这是自学 obj-c UITableView弄的,obj-c也是初次接触 很多东西还不知道怎么使用,大牛有兴趣还望指点一下!
里面右边的lable全部都是写死的 坐标,最好的方式应该是根据lable的文字长度来计算像素在来确定控件的左边,但是现在还不知道怎么根据控件的文本来计算像素,暂时只能写死!