今天的内容是在上一篇文章(<<iOS Dev 深入浅出 导航控制器(一)>>)的程序上,进行进一步操作,主要是为上一个程序添加更多的功能,来描述表视图控制器,导航控制器和表视图控制器是我们iOS应用中最常用的两个控制器,今天的内容呢,是围绕表视图控制器的相关操作进行撰写,因为我们已经了解导航控制器的操作了.我们现在为这个程序添加更多的二级视图.
首先一个SecondSingleMarkViewController(.h,.m),它同样继承于我们的统一二级管理控制器MySecondViewController,这个的主要功能是,实现TableView内的单选操作,我们会看到选择行后面会有一个"对号".首先,我们将SecondSingleMarkViewController导入MyBaseViewController和MySecondViewController,并在MyBaseViewController中像添加FirstShowMoreViewController一样添加它:
SecondSingleMarkViewController *secondSingleMarkViewController = [[SecondSingleMarkViewController alloc] initWithStyle:UITableViewStylePlain];
secondSingleMarkViewController.title = @"SingleMark";
secondSingleMarkViewController.ImageForRow = [UIImage imageNamed:@"checkmarkControllerIcon.png"];
[arrayforcontrollers addObject:secondSingleMarkViewController];
这样之后我们就可以专心的去写SecondSingleMarkViewController了,同样的添加显示数据,同样的让它显示出来,倒是这次们要添加这个对号扩展图标,所以我们在-(UITableViewCell *)tableView: cellForRowAtIndexPath:方法中添加上:
cell.accessoryType = ([lastindexpath row] == [indexPath row]&&lastindexpath !=nil)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
这里我们在h文件里面,声明了一个NSIndexPath *lastindexpath,它主要用来记录最后选择的行,如果没有它,那么在页面内滑动,会看到"对号位置不准确",之后开始添加方法-(void)tableView:didSelectRowAtIndexPath:来完成单选操作,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger newRow = [indexPath row];
NSInteger lastRow = (lastindexpath != nil)?[lastindexpath row]:-1;
if (newRow != lastRow) {
UITableViewCell *newcell = [tableView cellForRowAtIndexPath:indexPath];
newcell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *lastcell = [tableView cellForRowAtIndexPath:lastindexpath];
lastcell.accessoryType = UITableViewCellAccessoryNone;
self.lastindexpath = indexPath;
}
//取消选择,既使选择行不高亮
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
这样,我们的
SecondSingleMarkViewController就完成了,运行起来如图:
现在我们再添加一个二级视图控制器ThirdCostomAccessoryViewController(.h,.m)同样继承我们的统一二级管理控制器MySecondViewController,这个的主要功能是,实现自定义Cell的Accessory Icon.首先,我们将ThirdCostomAccessoryViewController导入MyBaseViewController和MySecondViewController,并在MyBaseViewController中像添加FirstShowMoreViewController一样添加它:
ThirdCostomAccessoryViewController *thirdCostomAccessoryViewController = [[ThirdCostomAccessoryViewController alloc] initWithStyle:UITableViewStylePlain];
thirdCostomAccessoryViewController.title = @"CostomAccessory";
thirdCostomAccessoryViewController.ImageForRow =[UIImage imageNamed:@"rowControlsIcon.png"];
[arrayforcontrollers addObject:thirdCostomAccessoryViewController];
之后的工作就是编辑ThirdCostomAccessoryViewController,又是同样的创建数据,和显示列表,在选择行时候触发动作,显示一个UIAlertView对象,并[alertview show]这都很简单,下面我们开始写这个自定义的扩展图标:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *pathindex = @"pathindex";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pathindex];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pathindex];
//初始化Button
UIImage *imageup = [UIImage imageNamed:@"button_up.png"];
UIImage *imagedown = [UIImage imageNamed:@"button_down.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, imageup.size.width, imageup.size.height);
[button setTitle:@"But" forState:UIControlStateNormal];
[button setBackgroundImage:imageup forState:UIControlStateNormal];
[button setBackgroundImage:imagedown forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(ButtonCilcked:) forControlEvents:UIControlEventTouchUpInside];
//这里的方法用来自定义扩展图标,而accessoryType是用来调用系统定义的4中样式
cell.accessoryView = button;
}
cell.textLabel.text = [arraylist objectAtIndex:[indexPath row] ];
//这里我们给每个button一个标签,一便我们在之后调用的时候找到它,这里是一个小技巧
UIButton *button = (UIButton *)cell.accessoryView;
button.tag = [indexPath row];
return cell;
}
再来写一下按键事件ButtonCilcked:方法
-(IBAction)ButtonCilcked:(id)sender{
UIButton *button = sender;
NSString *buttonTitle = [arraylist objectAtIndex:button.tag];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You choose:" message:buttonTitle delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
这样我们就完成了工作,
这样我们这个程序就有了三个功能,功能还有,我会继续写下去