【问题标题】:Xcode how to link UITableView Cells to a new View ControllerXcode 如何将 UITableView 单元链接到新的视图控制器
【发布时间】:2012-09-07 21:10:21
【问题描述】:

我目前开发了一个基于选项卡的应用程序。第一个选项卡是在 TableView 结构中显示颜色样本或图像的装饰。当前,当您推送图像或颜色样本时,会弹出一条警报,说明您推送了哪个表格单元格。相反,我想将每个表格单元格图像或颜色样本链接到一个新的视图控制器,以显示该图像或颜色样本的更大图像。模态也可以

#import "TableViewsViewController.h"

@implementation TableViewsViewController

#pragma mark -
#pragma mark Synthesizers

@synthesize table;
@synthesize sitesArray;
@synthesize imagesArray;

#pragma mark -
#pragma mark View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {


// Load up the sitesArray with a dummy array : sites
NSArray *sites = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", nil];
self.sitesArray = sites;
[sites release];

UIImage *active = [UIImage imageNamed:@"a.png"];
UIImage *ae = [UIImage imageNamed:@"b.png"];
UIImage *audio = [UIImage imageNamed:@"c.png"];
UIImage *mobile = [UIImage imageNamed:@"d.png"];
UIImage *net = [UIImage imageNamed:@"e.png"];
UIImage *photo = [UIImage imageNamed:@"f.png"];
UIImage *psd = [UIImage imageNamed:@"g.png"];
UIImage *vector = [UIImage imageNamed:@"h.png"];

NSArray *images = [[NSArray alloc] initWithObjects: active, ae, audio, mobile, net, photo, psd, vector, nil];
self.imagesArray = images;
[images release];

[super viewDidLoad];
}


#pragma mark -
#pragma mark Table View datasource methods

// Required Methods

// Return the number of rows in a section
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [sitesArray count];
}

// Returns cell to render for each row
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure cell

NSUInteger row = [indexPath row];

// Sets the text for the cell
//cell.textLabel.text = [sitesArray objectAtIndex:row];

// Sets the imageview for the cell
cell.imageView.image = [imagesArray objectAtIndex:row];

// Sets the accessory for the cell
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

// Sets the detailtext for the cell (subtitle)
//cell.detailTextLabel.text = [NSString stringWithFormat:@"This is row: %i", row + 1];

return cell;
}

// Optional

// Returns the number of section in a table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

#pragma mark -
#pragma mark Table View delegate methods

// Return the height for each cell
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}

// Sets the title for header in the tableview
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Decors";
}

// Sets the title for footer
-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"Decors";
}

// Sets the indentation for rows
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
 {
return 0;
}

// This method is run when the user taps the row in the tableview
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tapped row!" 
                                          message:[NSString stringWithFormat:@"You tapped: %@", [sitesArray objectAtIndex:indexPath.row]]
                                          delegate:nil 
                                          cancelButtonTitle:@"Yes, I did!" 
                                          otherButtonTitles:nil];
[alert show];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
NSLog(@"Memory Warning!");
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
self.table = nil;
self.sitesArray = nil;
self.imagesArray = nil;
[super viewDidUnload];
}


- (void)dealloc {
[table release];
[sitesArray release];
[imagesArray release];
[super dealloc];
}

@end

警报所在的部分

// This method is run when the user taps the row in the tableview
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tapped row!" 
                                          message:[NSString stringWithFormat:@"You tapped: %@", [sitesArray objectAtIndex:indexPath.row]]
                                          delegate:nil 
                                          cancelButtonTitle:@"Yes, I did!" 
                                          otherButtonTitles:nil];
[alert show];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

【问题讨论】:

    标签: iphone xcode xcode4 uitableview


    【解决方案1】:

    didSelectRowAtIndexPath 中,您可以只初始化另一个视图控制器并呈现。您可以通过self.navigationController 展示它,以便您可以根据需要使用后退按钮。在这里,我以模态方式展示它:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // Deselect row
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        // Declare the view controller
        UIViewController *anotherVC = nil;
    
        // Determine the row/section on the tapped cell
        switch (indexPath.section) {
            case 0:
                switch (indexPath.row) {
                    case 0: {
                        // initialize and allocate a specific view controller for section 0 row 0
                        anotherVC = [[ViewControllerForRowZeroSectionZero alloc] init];
                        break;
                    }
                    case 1: {
                        // initialize and allocate a specific view controller for section 0 row 1
                        anotherVC = [[ViewControllerForRowOneSectionZero alloc] init];
                        break;
                    }
                }
                break;
            case 1: {
                // initialize and allocate a specific view controller for section 1 ALL rows
                anotherVC = [[ViewControllerForAllRowsSectionOne alloc] init];
                break;
            }
        }
    
        // Get cell textLabel string to use in new view controller title
        NSString *cellTitleText = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
    
        // Get object at the tapped cell index from table data source array to display in title
        id tappedObj = [sitesArray objectAtIndex:indexPath.row];
    
        // Set title indicating what row/section was tapped
        [anotherVC setTitle:[NSString stringWithFormat:@"You tapped section: %d - row: %d - Cell Text: %@ - Sites: %@", indexPath.section, indexPath.row, cellTitleText, tappedObj]];
    
        // present it modally (not necessary, but sometimes looks better then pushing it onto the stack - depending on your App)
        [anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
    
        // Have the transition do a horizontal flip - my personal fav
        [anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    
        // The method `presentModalViewController:animated:` is depreciated in iOS 6 so use `presentViewController:animated:completion:` instead.
        [self.navigationController presentViewController:anotherVC animated:YES completion:NULL];
    
        // We are done with the view controller.  It is retained by self.navigationController so we can release it (if not using ARC)
        [anotherVC release], anotherVC = nil;
    }
    

    【讨论】:

    • 我有点困惑?它如何知道要转到哪个视图控制器?我必须为每个图像复制上面的代码吗?在我的应用程序的警报中, stringWithFormat:@"You tapped: %@" 告诉我点击了哪个图像。 [NSString stringWithFormat:@"You tapped: %@", [sitesArray objectAtIndex:indexPath.row] 所以了解警报但仍然无法链接表格
    • 查看我的更新。您可以根据 indexPath 显示不同的控制器。甚至可以使用相同的控制器,但根据部分/行将其视图的属性设置为不同的值。
    • 仍在忙于输入代码。我一来就告诉你。但这似乎是正确的,我理解它。谢谢你。
    • 我现在理解了代码,但是因为我是 xcode 的新手,所以我不理解视图控制器和我必须设置的 nib 文件之间的关系。如果您查看我的项目所在的链接,您可以运行我根据需要编辑的测试项目。也许您可以帮助我编辑我的项目,以帮助我了解您如何将界面构建器项目与代码链接。对此我将非常感谢您。我制作了一个标签栏应用程序,并打算将此代码集成到我的项目中。 www.blakeloizides.co.za/table.zip,这个实用的帮助会去
    • 在帮助我理解方面还有很长的路要走。
    【解决方案2】:

    您已经完成了 90% 的任务!在didSelectRowAtIndexPath 中,不要发出警报,而是实例化并设置要显示其视图的视图控制器并调用presentViewController:animated:

    看我的书:http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller

    【讨论】:

      【解决方案3】:

      你在使用 Storyboard 吗? 如果是这样,您可以构建一个 segue,将其设置为 Modal 或 Push。从 TableView 指向目标 ViewController 然后在 segue 的检查器中用标识符命名它。 然后在didSelectRowAtIndexPath中,调用performSegue:withIdentifier 您还可以根据在 prepareForSegue 中选择的单元格设置目标 ViewController。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-11
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多