iphone - 单击 TableView 会将您带到另一个 View

标签 iphone ios uitableview

我是 iPhone 开发的新手。我已经创建了一个 UITableView。我已将所有内容连接起来并包括 delegatedatasource。但是,我不想使用 UITableViewCellAccessoryDe​​tailClosureButton 添加详细 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/

相关文章:

iphone - 如何在半圆形图案中绘制点

ios - 合并或附加两个数据模型相同的结构以在 Swift 中发布值

objective-c - iOS动态填充搜索结果

iphone - 在 TableView 中显示图像

iphone - 使用开源库中的 Instagram 过滤器

javascript - 出现键盘时 UIWebView 滚动。导致抽头偏移错误?

iphone - 为什么应用内购买沙盒总是询问 "Verification Required"?

iphone - 如何在后台线程上定期从服务器轮询数据?

ios - Xcode/Parse 未获取测试用户数据 [Parse Starter Kit 1.9.0 + 1.9.1]

ios - 无法在 IOS8 中使用 showSegue 以编程方式 "push"viewcontroller