UITableView+UISwitch实现对新闻显示的设置
2012-07-19 19:02
316 查看
新闻显示页面的设置,主要实现以下功能:1.一个UITableView页作为总的设置页,点击新闻显示,进入新闻详情设置页面,详情页面包含以下设置:是否显示图片,是否显示作者,是否显示日期,是否显示简介,调整图片位置(左或右)。2.用户的设置被保存到plist文件,每次都先从plist配置文件中读取。3.只有在用户选择了显示图片时才可对图片位置进行设定,否则“图片设置”开关被隐藏。直接上代码,第一个.h文件:
#import <UIKit/UIKit.h> #import "detailSettingViewController.h"//注意一定在头文件添加要跳转到的页面 @interface settingViewController : UITableViewController { }//这里并不需要添加UITableViewDelegate和UITableViewDataSource,因为本身就继承自UITableView。 @property(nonatomic,retain)IBOutlet UITableView *settingTableView; @end第一个.m文件
#import "settingViewController.h" #import <QuartzCore/QuartzCore.h> @interface settingViewController () @end @implementation settingViewController @synthesize settingTableView=_settingTableView; - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 1; } - (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]; } cell.imageView.layer.cornerRadius=12.0; cell.imageView.layer.masksToBounds=YES; cell.imageView.image=[UIImage imageNamed:@"Icon.png"]; cell.textLabel.text=@"新闻显示"; return cell; }@end第二个.h文件:
#import <UIKit/UIKit.h> @interface detailSettingViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{ NSMutableArray *sectionArray; NSMutableArray *rowArray; NSMutableDictionary *newsUserDefault; BOOL showImage;//是否显示图片 BOOL imageLocation;//图片的位置,左或右 BOOL showAuthor;//是否显示作者 BOOL showDate;//是否显示日期 BOOL showIntro;//是否显示简介 NSString *plistPath;//文件存储位置 } @property(nonatomic,retain)IBOutlet UITableView *detailSettingTableView; @end这里有一个特别需要注意的地方,我之前直接继承自UITableViewController,出现了以下错误提示:[UITableViewControllerloadView] loaded the "2-view-3" nib but didn't get a UITableView.'原因:项目中存在多个ViewController,每个ViewController 都有UITableView,点击前一个ViewController 的UITableView 的cell跳转到第二个ViewController 的UITableView。第一个ViewController直接继承自UITableViewController,而第二个ViewController也继承自UITableViewController,这样就导致了歧义,系统在loadView的时候就不知道该给哪一个ViewController loadView了。解决办法:将第二个ViewController继承自UIViewController。补充说明:第一个ViewController继承自UITableViewController,所以不再需要实现UITableViewDelegate和UITableViewDataSource。而第二个ViewController则需要实现这两个协议。可参见:http://stackoverflow.com/questions/10549337/uisearchbar-and-uitableview。接下来是.m文件:
#import "detailSettingViewController.h" @interface detailSettingViewController () @end @implementation detailSettingViewController @synthesize detailSettingTableView=_detailSettingTableView; -(void)ArrayInit { sectionArray=[[NSMutableArray alloc] initWithObjects:@"显示设置",@"图片位置" ,nil]; rowArray=[[NSMutableArray alloc] initWithObjects:@"图片",@"日期",@"作者",@"简介", nil]; } -(void)readThePlist { plistPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"userDefaults.plist"]; if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { newsUserDefault=[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]; imageLocation=[[newsUserDefault valueForKey:@"imageLoaction"] boolValue]; showImage=[[newsUserDefault valueForKey:@"showImage"] boolValue]; showDate=[[newsUserDefault valueForKey:@"showDate"] boolValue]; showAuthor=[[newsUserDefault valueForKey:@"showAuthor"] boolValue]; showIntro=[[newsUserDefault valueForKey:@"showIntroduction"] boolValue]; } else //创建文件 { NSLog(@"create"); newsUserDefault=[[NSMutableDictionary alloc] initWithCapacity:12]; [newsUserDefault writeToFile:plistPath atomically:YES]; } } - (void)viewDidLoad { [super viewDidLoad]; [self ArrayInit]; [self readThePlist]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [sectionArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (section==0) { return [rowArray count]; }else { return 1; } } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { //section header tittle NSString *sectionHeader=[[NSString alloc] init]; sectionHeader=[sectionArray objectAtIndex:section]; return sectionHeader; } -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { //section footer NSString *sectionFooter=[[NSString alloc]init]; if (section==0) { sectionFooter=@"是否显示"; }else { sectionFooter=@"图片显示在左或右"; } return sectionFooter; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger section=indexPath.section; NSInteger row=indexPath.row; //@我本想干掉cell重用机制,后面发现这样很傻,但请注意,总有不得已的时候。 // NSString *CellIdentifier =[NSString stringWithFormat:@"cell%d%d",section,row]; // UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; static NSString *CellIdentifier=@"Cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } UISwitch *Switch=[[UISwitch alloc] initWithFrame:CGRectMake(220, 6, 79, 27)]; [Switch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; if (section==0) { Switch.tag=101+row; cell.textLabel.text=[rowArray objectAtIndex:row]; switch (Switch.tag) { case 101: Switch.on=showImage; break; case 102: Switch.on=showDate; break; case 103: Switch.on=showAuthor; break; case 104: Switch.on=showIntro; break; default: break; } [cell.contentView addSubview:Switch]; }else if(section==1){ Switch.tag=100; Switch.hidden=YES; if (showImage) { Switch.hidden=NO; Switch.on=imageLocation; if (imageLocation==0) { NSLog(@"图片显示在左边"); }else { NSLog(@"图片显示在右边"); } } cell.textLabel.text=@"Left/Right"; [cell.contentView addSubview:Switch]; } return cell; } -(void)switchAction:(UISwitch *)sender { NSInteger switchTag=sender.tag; BOOL switchStatus=sender.on;//Switch的状态 NSNumber *convertSwitchStatus=[[NSNumber alloc] initWithBool:switchStatus]; UISwitch *imageSwitch=(UISwitch *)[self.view viewWithTag:100];//图片可显示时才能调整图片的位置 switch (switchTag) { case 101: [newsUserDefault setValue:convertSwitchStatus forKey:@"showImage"]; if (switchStatus==YES) { imageSwitch.hidden=NO; }else { imageSwitch.hidden=YES; } break; case 102: [newsUserDefault setValue:convertSwitchStatus forKey:@"showDate"]; break; case 103: [newsUserDefault setValue:convertSwitchStatus forKey:@"showAuthor"]; break; case 104: [newsUserDefault setValue:convertSwitchStatus forKey:@"showIntroduction"]; break; case 100: [newsUserDefault setValue:convertSwitchStatus forKey:@"imageLoaction"]; break; default: break; } // NSLog(@"-----plist-----%@,count=%i",newsUserDefault,[newsUserDefault count]); [self saveToPlistFile]; } -(void)saveToPlistFile { NSString *localPath=[[NSBundle mainBundle] pathForResource:@"userDefaults" ofType:@"plist"]; plistPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"userDefaults.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { [[NSFileManager defaultManager] copyItemAtPath:localPath toPath:plistPath error:nil]; } [newsUserDefault writeToFile:plistPath atomically:YES]; } @end程序运行的效果如下:点击之后进入详情设置页面:ps:一定要养成好的编程习惯啊,就是上面那个switch:case语句里的一个break忘了写,调试我一下午,多么苦逼的错误啊,希望各位以此为戒。另外,也许有的人会精益求精的觉得在图片位置那个UISwitch中是否可以显示“左/右”来标识用户的选择。当然,这样做的确是做到了更好的用户体验。我也尝试了一些方法,大致思路就是利用Objective C的特性之一的Category,对UISwitch进行扩展,这一点可参考:/article/4343665.html和http://www.flyblog.info/catprogramming/258.html。但是后面一位朋友的实现方法会卡死在下面这一句,网上说这种方法在新的SDK中不能再用了,请各位大侠指点。我用的是最新的SDK。
return [[[self slider] subviews] objectAtIndex:2];
相关文章推荐
- 解决方案实现在UITableView中显示数据
- iOS开发>>>UITableview实现更多数据显示方法
- UITableView实现分页显示
- Swift UITableView相关功能(三)设置每行显示的内容
- UItableView实现分页效果(点击more再显示10条)
- iOS-UITableView 实现分页显示的代码
- 用RecyclerView实现新闻列表页,包括头部的图片轮播,两种Item显示方式,下拉刷新和上拉加载以及限制列表的加载条目数
- iphone开发 UITableview实现分页显示方法(代码)
- IOS中通过UICollectionView和UICollectionViewFlowLayout设置初始游标cursor来控制左右滑动来实现多条记录水平分页显示及控制音标读音
- UITableview里面对于分割线显示整个屏幕宽度的设置
- 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题
- IOS开发- UITableView 无数据时,显示“暂无数据”背景的实现
- UITableView 实现分页显示的代码
- UITableView 实现分页显示的代码
- [转]UITableView 实现分页显示的代码
- android banner view的实现,无限循环,可以设置要显示的个数
- iOS设置UITableView的背景图片,以及不显示多余的空Cell
- UITableView--单组数据显示(纯手动代码实现)
- iOS如何完美简单实现UITableView索引的放大悬浮提示View显示
- Android自定义控件View(实现控件的动画效果,自定义类画简略时钟,TextView中文字逐一显示,动态设置progress的圆环/扇形ProgressBar等)