iOS开发:导航控制器与表格视图的多样化实现
立即解锁
发布时间: 2025-08-24 01:05:38 阅读量: 1 订阅数: 6 

### iOS 开发:导航控制器与表格视图的多样化实现
在 iOS 开发中,导航控制器和表格视图是非常重要的组件。下面将详细介绍如何实现不同功能的表格视图,包括单选列表、带按钮的表格行以及可移动的表格行。
#### 1. 单选列表的实现
在表格视图中,为了实现单选功能,我们需要对选中行添加复选标记,并确保同一时间只有一行被选中。以下是关键代码:
```objc
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
```
在 `tableView:didSelectRowAtIndexPath:` 方法中,我们需要处理选中行的切换:
```objc
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
```
要将这个单选列表控制器添加到 `BIDFirstLevelController` 中,需要进行以下操作:
1. 导入头文件:
```objc
#import "BIDCheckListController.h"
```
2. 在 `viewDidLoad` 方法中创建并添加控制器实例:
```objc
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"First Level";
NSMutableArray *array = [[NSMutableArray alloc] init];
// Disclosure Button
BIDDisclosureButtonController *BIDDisclosureButtonController =
[[BIDDisclosureButtonController alloc] initWithStyle:UITableViewStylePlain];
BIDDisclosureButtonController.title = @"Disclosure Buttons";
BIDDisclosureButtonController.rowImage = [UIImage imageNamed:@"BIDDisclosureButtonControllerIcon.png"];
[array addObject:BIDDisclosureButtonController];
// Checklist
BIDCheckListController *checkListController = [[BIDCheckListController alloc] initWithStyle:UITableViewStylePlain];
checkListController.title = @"Check One";
checkListController.rowImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"];
[array addObject:checkListController];
self.controllers = array;
}
```
保存更改并编译运行,应用程序将显示两个行,点击 “Check One” 行将进入单选列表视图。
#### 2. 带按钮的表格行实现
为了在表格行中添加按钮,我们需要创建一个新的控制器 `BIDRowControlsController`。
首先,在 `BIDRowControlsController.h` 中进行如下修改:
```objc
#import "BIDSecondLevelViewController.h"
@interface BIDRowControlsController : BIDSecondLevelViewController
@property (strong, nonatomic) NSArray *list;
- (IBAction)buttonTapped:(id)sender;
@end
```
然后,在 `BIDRowControlsController.m` 中实现具体逻辑:
```objc
#import "BIDRowControlsController.h"
@implementation BIDRowControlsController
@synthesize list;
- (IBAction)buttonTapped:(id)sender {
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
NSUInteger buttonRow = [[self.tableView indexPathForCell:buttonCell] row];
NSString *buttonTitle = [list objectAtIndex:buttonRow];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"You tapped the button"
message:[NSString stringWithFormat:@"You tapped the button for %@", buttonTitle]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"R2-D2",
@"C3PO", @"Tik-Tok", @"Robby", @"Rosie", @"Uniblab",
@"Bender", @"Marvin", @"Lt. Commander Data",
@"Evil Brother Lore", @"Optimus Prime", @"Tobor", @"HAL",
@"Orgasmatron", nil];
self.list = array;
}
- (void)viewDidUnload {
[super viewDidUnload];
self.list = nil;
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ControlRowIdentifier = @"ControlRowIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ControlRowIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ControlRowIdentifier];
UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, buttonUpImage.size.height);
[button setBackgroundImage:buttonUpImage forState:UIControlS
```
0
0
复制全文
相关推荐










