fly*_*ers 11 objective-c uitableview uiviewcontroller ios uistoryboardsegue
我在View Controller中有一个表视图.我可以在表视图中填充所有信息.但是我在设置详细视图时有点迷失.我相信每个表格单元格需要一个segue到每个细节视图但不完全确定.
这是我的代码.从表格视图到详细视图完成segue我缺少什么?码:
.h
@interface DetailViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *myTable;
NSMutableArray *contentArray;
}
@property (strong, nonatomic) IBOutlet UITableView *myTable;
.m
- (void)viewDidLoad
{
contentArray = [[NSMutableArray alloc]init];
[contentArray addObject:@"Espresso"];
[contentArray addObject:@"Latte"];
[contentArray addObject:@"Capicino"];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//Table Information
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [contentArray count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([[contentArray objectAtIndex:indexPath.row]isEqualToString:@"EspressoViewController"])
{
EspressoViewController *espresso = [[EspressoViewController alloc]initWithNibName:@"EspressoViewController" bundle:nil];
[self.navigationController pushViewController:espresso animated:YES];
}
else if ([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"Latte"])
{
LatteViewController *latte = [[LatteViewController alloc] initWithNibName:@"Latte" bundle:nil];
[self.navigationController pushViewController:latte animated:YES];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
}
NSString *cellValue = [contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.detailTextLabel.text = @"Hot and ready";
UIImage *image = [UIImage imageNamed:@"coffeeButton.png"];
cell.imageView.image = image;
cell.textLabel.text = [contentArray objectAtIndex:indexPath.row];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
小智 17
我觉得你这太复杂了.别担心,我经常做同样的事情.
首先,通过tableView:didSelectRowAtIndexPath:从内部发送,tableView:accessoryButtonTappedForRowAtIndexPath:两种方法之间没有区别.点击单元格或其附件按钮执行相同的操作.如果您不需要配件按钮执行与点击电池本身不同的操作,请将其取下.
其次,如果您使用的是故事板,则不需要为视图控制器分配/ initWithNib.相反,使用segue.如果通过故事板执行此操作,则还无需以编程方式将viewControllers推送到navigationController
首先构建故事板:
然后将以下代码放在UITableViewController中:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
//Build a segue string based on the selected cell
NSString *segueString = [NSString stringWithFormat:@"%@Segue",
[contentArray objectAtIndex:indexPath.row]];
//Since contentArray is an array of strings, we can use it to build a unique
//identifier for each segue.
//Perform a segue.
[self performSegueWithIdentifier:segueString
sender:[contentArray objectAtIndex:indexPath.row]];
}
Run Code Online (Sandbox Code Playgroud)
你如何实现其余部分取决于你.您可能希望prepareForSegue:sender:在UITableViewController中实现,然后使用该方法将信息发送到segue.destinationViewController.
请注意,我从您的contentArray传递了字符串作为segue的发件人.你可以传递任何你喜欢的东西.标识单元格的字符串似乎是最合理的信息,但选择取决于您.
上面发布的代码应该为您执行导航.
| 归档时间: |
|
| 查看次数: |
27562 次 |
| 最近记录: |