IOS:通过 tableView 导航并在下一个视图上显示表视图数据 [英] IOS: Navigate through tableView and show table view data on next view

查看:25
本文介绍了IOS:通过 tableView 导航并在下一个视图上显示表视图数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用了一个自定义的 UITableView,其中我传递了 2 个显示在 UITableView 上的数组(图像和名称)数组.现在我想当我点击表格的任何单元格时,UITableViewCell 的凭据将显示在下一个视图中,我在其中使用 UIImageView(用于图像)和 UILabel(用于名称显示).我还导入了必须在其上显示图像和名称的 VC.请建议我在另一个视图上显示名称和图像的代码

in my project i am using a custom UITableView in which i passes 2 array (image and name) arrays are showing on UITableView. Now i want to when i click on any cell of table view the credentials of UITableViewCell to be display on next View in which i take an UIImageView (for image) and UILabel (for name display). i also import the VC on which i have to display image and name. please suggest me code to display name and image on another view

我正在使用以下代码

.h 文件

#import <UIKit/UIKit.h>

@interface FriendsViewController : UIViewController<UISearchDisplayDelegate , UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

    {
        UITableView *friendslisttable;
        NSMutableArray *friendslist;
        NSMutableArray *friendsnamearray;
        NSMutableArray *profilepicarray;
        UIImageView * frndprofpic;
        UILabel *friendnamelabel;

        NSArray *searchResults;
    }
    @end

.m 文件

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.title = @"Friends";

    friendslisttable = [[UITableView alloc]initWithFrame:CGRectMake(0,110 , self.view.frame.size.width, self.view.frame.size.height)] ;
    friendslisttable.delegate = self;
    friendslisttable. dataSource = self;
    [self.view addSubview:friendslisttable];

    friendsnamearray = [[NSMutableArray alloc]initWithObjects:@"friend1",@"friend12",@"friend13",@"friend14",@"friend15",@"friend16",@"friend17",@"friend18",@"friend19",@"friend20",@"friend21 ",@"friend22",@"", nil];

    profilepicarray = [[NSMutableArray alloc]initWithObjects:@"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", @"download13.jpeg", @"download16.jpeg",@"download3.jpeg", @"download6.jpg", @"download12.jpeg", @"download16.jpeg", @"  ",  nil];
}

#pragma mark - TableView Delegate Method -------------------->>>>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return searchResults.count;
    }
    else {
        return friendsnamearray.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellidentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];

    if(!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    friendslisttable  = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        friendslisttable = [searchResults objectAtIndex:indexPath.row];
    } else
    {
        friendslisttable = [friendsnamearray objectAtIndex:indexPath.row];
    }

    frndprofpic = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];

    frndprofpic.image=[UIImage imageNamed:[profilepicarray objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:frndprofpic];

    friendnamelabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 250, 50)];
    friendnamelabel.text = [friendsnamearray objectAtIndex:indexPath.row];
    friendnamelabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];
    [cell.contentView addSubview:friendnamelabel];

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.row) {
        ProfileViewController *u = [[ProfileViewController alloc]init];
        [self.navigationController pushViewController:u animated:YES];
    }
}

推荐答案

对于像您这样的情况,我建议您将表格视图单元格子类化.在您的案例中为个人资料图片和名称创建 2 个属性.覆盖这些属性的设置器并根据需要设置视图组件.

For the cases such as yours I suggest you to subclass the table view cell. Create 2 properties in your case for the profile image and for the name. Override setters of these properties and set the view components as needed.

在 did select row 事件中,您现在可以使用自定义单元格的相同属性,这些属性可以在推送之前设置为目标控制器的属性.

On the event of did select row you may now use the same properties of the custom cell which can be set as properties to your target controller before pushing it.

但是如果你想正确地做它,你应该总是使用一个模型类(MyUserClass),然后可以将它插入到自定义单元格和视图控制器中.这样你就只为输入和输出保留一个属性.此外,如果您的模型类发生变化,或者您需要显示更多数据,那么大部分代码都不会发生变化.例如,假设您的单元格保持不变,但您还希望在视图控制器上添加用户位置:您需要做的就是将位置属性添加到模型类,然后仅在视图控制器中使用它.(如果您使用多个属性执行此操作,则即使不显示该属性,单元格也需要收集该属性).

But if you want to do it proper you should always use a model class (MyUserClass) which can then be inserted into the custom cell and the view controller. This way you keep only one property for both the input and output. Also if your model class changes or if you need to display more data not much of the code will change. For instance imagine your cell stays the same but on the view controller you wish to also add the user location: All you need to do is add the location property to the model class and then use it only in the view controller. (if you did it with multiple properties the cell would need that property to be collected even though it does not display it).

让我给你一个很好的例子,说明与表视图、表视图单元格和所有者(通常是视图控制器)通信的一些用途.

这是一个展示如何很好地从单元格获取数据以及如何将自定义控件(按钮)挂接到单元格并用适当的数据通知事件的所有者.

This is a display of how to get the data from cell nicely as well as hooking a custom control (the button) to the cell and notify the owner of the event with the appropriate data.

请注意,这一切都是以编程方式完成的,因此您可以看到所有已完成的代码.不过,这一切都可以通过故事板来完成.

Note this is all done programmatically so you can see all the code done. This may all be done with storyboards though.

标题:

#import <UIKit/UIKit.h>

@class MyModel;
@class MyTableViewCell;

@interface MyViewController : UIViewController

- (void)specialCellButtonPressed:(MyTableViewCell *)sender selectedModel:(MyModel *)model;

@end

来源:

#import "MyViewController.h"


#pragma mark - My model
/*
 MyModel:
 Model that we will use to hold the data
*/
@interface MyModel : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *descriptionText;

@end

#pragma mark - My model
/*
 MyTableViewCell:
 Custom table view cell to show the MyModel data
 */
@interface MyTableViewCell : UITableViewCell

@property (nonatomic, strong) MyModel *model;

@property (nonatomic, strong) UIButton *specialButton; // have the secondary action
@property (nonatomic, weak) MyViewController *owner; // note this may also be solved with a custom delegate but we want to bind this one to a concrete class that has type of MyViewController. Be careful to set this property to "weak"

@end

@implementation MyTableViewCell

- (void)setModel:(MyModel *)model
{
    _model = model; // standard asignment

    // set some views to show the actual data
    self.textLabel.text = model.title;
    self.detailTextLabel.text = model.descriptionText;
}

#pragma mark - custom button on the cell

// lazy load for the button
- (UIButton *)specialButton
{
    if(_specialButton == nil)
    {
        // put the button on the right side of the frame
        _specialButton = [[UIButton alloc] initWithFrame:CGRectMake(.0f, .0f, self.contentView.frame.size.width*.5f, self.contentView.frame.size.width*.5f)];
        [_specialButton addTarget:self action:@selector(spectialButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_specialButton];
    }
    return _specialButton;
}

#pragma mark - layout

- (void)layoutSubviews
{
    [super layoutSubviews];
    // reposition the custom view if needed
    self.specialButton.frame = CGRectMake(.0f, .0f, self.contentView.frame.size.width*.5f, self.contentView.frame.size.width*.5f);
}

#pragma mark - actions

- (void)spectialButtonPressed:(id)sender
{
    // just report this to the delegate or rather the owner in this case
    [self.owner specialCellButtonPressed:self selectedModel:self.model];
}

@end


#pragma mark - My model
/*
 MyViewController:
 A view controller showing the table view
 */
@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *models;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MyViewController

#pragma mark - lifetime

- (void)viewDidLoad {
    [super viewDidLoad];

    // fill some data for our table view
    NSMutableArray *models = [[NSMutableArray alloc] init];
    for(NSInteger i=0; i<24; i++)
    {
        MyModel *model = [[MyModel alloc] init];
        model.title = [NSString stringWithFormat:@"Cell %d", (int)(i+1)];
        model.descriptionText = [NSString stringWithFormat:@"This is a cell with index: %d", (int)(i)];
        [models addObject:model];
    }
    self.models = models;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

// lazy load from table view
- (UITableView *)tableView
{
    if(_tableView == nil)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(.0f, .0f, self.view.frame.size.width, self.view.frame.size.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - layout

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    // reposition the table view if needed
    self.tableView.frame = CGRectMake(.0f, .0f, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.model = self.models[indexPath.row]; // assign the model from our list
    cell.owner = self;
    return cell;
}

// standard cell selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    MyModel *model = cell.model;
    /*
     Alternative if we have the data array (as we do in this case):

     MyModel *model = self.models[indexPath.row];

     */

    NSLog(@"Selected model: %@", model);
    // do whatever with the model
}

#pragma mark - special

- (void)specialCellButtonPressed:(MyTableViewCell *)sender selectedModel:(MyModel *)model
{
    NSLog(@"Special button for model pressed: %@", model);
}

@end




@implementation MyModel

// override description to get some usefull data
- (NSString *)description
{
    return [NSString stringWithFormat:@"<%p> %@ (%@)", self, self.title, self.descriptionText];
}

@end

这篇关于IOS:通过 tableView 导航并在下一个视图上显示表视图数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆