您的位置:首页 > 产品设计 > UI/UE

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.htmlhttp://www.flyblog.info/catprogramming/258.html。但是后面一位朋友的实现方法会卡死在下面这一句,网上说这种方法在新的SDK中不能再用了,请各位大侠指点。我用的是最新的SDK。
return [[[self slider] subviews] objectAtIndex:2];

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐