2012-10-28 23 views
0

我是iPhone開發的新手。我已經創建了一個UITableView。我已將所有內容接通,包括delegatedatasource。但是,我不想通過使用UITableViewCellAccessoryDetailClosureButton來添加詳細視圖附件,而是想點擊UITableViewCell,它應該導致另一個視圖,其中包含有關UITableViewCell的更多詳細信息。 我的視圖控制器看起來是這樣的:點擊TableView會引導您到另一個視圖

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 

需要一些這方面的指導..謝謝..請原諒我,如果這是一個愚蠢的題。

回答

2

你應該實現以下方法在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]; 
} 

希望它能幫助:)

+0

哪裏你_dataSource和DetailViewController從何而來? – lakesh

+0

這僅僅是一個例子。在你的情況下,self.tableItems將是數據源,DetailViewController將是你的UIViewController的名字,你將用它來呈現你的詳細信息視圖。 –

+0

DetailViewController將是您的UIViewController的名稱,您將使用它顯示您的詳細信息視圖。這意味着我需要另一個ViewController,我有權這麼說嗎?例如,SecondViewController? – lakesh

0

實現的UITableViewDelegate方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 在一側的方法只需添加您的詳細UIView(assum e全球範圍內有參考並將此視圖分配給viewDidLoad:方法)至self.view。根據所選indexPath更新詳細視圖的內容。您可能還希望在詳細視圖上具有「完成」按鈕,可以使用該按鈕返回到您的表格視圖。

如果應用程序使用UINavigationViewController,有detailedViewController(的UIViewController型),而不是UIView並使用它推到導航控制器:

[self.navigationController pushViewController:detailedViewController animated:YES]; 
+0

我不使用UINavigationViewController在我的應用程序..然後如何? – lakesh

相關問題