我是 iPhone 开发的新手。我已经创建了一个 UITableView
。我已将所有内容连接起来并包括 delegate
和 datasource
。但是,我不想使用 UITableViewCellAccessoryDetailClosureButton
添加详细 View 附件,而是想单击 UITableViewCell
,它应该会导致另一个 View ,其中包含有关 UITableViewCell 的更多详细信息
。
我的 View Controller 看起来像这样:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
NSArray *tableItems;
NSArray *images;
}
@property (nonatomic,retain) NSArray *tableItems;
@property (nonatomic,retain) NSArray *images;
@end
ViewController.m
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize tableItems,images;
- (void)viewDidLoad
{
[super viewDidLoad];
tableItems = [[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3",nil];
images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"clock.png"],[UIImage imageNamed:@"eye.png"],[UIImage imageNamed:@"target.png"],nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Step 1:Check whether if we can reuse a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//If there are no new cells to reuse,create a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;
//changing the radius of the corners
//cell.layer.cornerRadius = 10;
}
//Set the image in the row
cell.imageView.image = [images objectAtIndex:indexPath.row];
//Step 3: Set the cell text content
cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
//Step 4: Return the row
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.backgroundColor = [ UIColor greenColor];
}
@end
需要一些指导。谢谢。如果这是一个愚蠢的问题,请原谅我。
最佳答案
您应该在 UIViewController 中实现以下方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[例子]
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ExampleDataItem *dataItem = [_dataSource objectAtIndex:[indexPath row]];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.dataSource = dataItem;
[self.navigationController pushViewController:detailViewController animated:YES];
}
希望对您有所帮助:)
关于iphone - 单击 TableView 会将您带到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13110116/