V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
v2Next
V2EX  ›  iDev

关于 UITableView 点击 Cell pushviewcontroller 传值的问题

  •  
  •   v2Next ·
    jinjian1991 · 2015-06-02 15:25:10 +08:00 · 5043 次点击
    这是一个创建于 3222 天前的主题,其中的信息可能已经有所发展或是发生改变。
    点击TableViewCell,Push到一个viewcontroller,push的那个viewcontroller里面的数据是要根据cell里面的数据而定的.我想问下,每个push后视图的数据都不同,那要怎么赋值传值?

    我的表格页面叫Tab2ViewController,是用xib ListTableViewCell画的,push到的页面叫DetailViewController。

    ```
    #pragma mark - TableView datasource
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    static NSString *CellIdentifier = @"ListTableViewCell";
    ListTableViewCell *cell = (ListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
    NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"ListTableViewCell" owner:self options:nil];
    cell = (ListTableViewCell *)[nibs objectAtIndex:0];
    [cell setValue:CellIdentifier forKey:@"reuseIdentifier"];

    }
    //**遍历respont**//
    //**liatArray是从服务器返回的数据**//
    NSMutableDictionary *tempDic = [listArray objectAtIndex:indexPath.row];

    cell.timeLabel.text = [NSString stringWithFormat:@"%@", [tempDic objectForKey:@"bno"]]; // 时间

    NSString *percentStr = [NSString stringWithFormat:@"%@", [tempDic objectForKey:@"apr"]];
    cell.percentLabel.text = percentStr;

    return cell;
    }


    #pragma mark - UITableView Delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 我想要在DetailViewController里面显示数据
    DetailViewController *detailViewController = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
    }
    ```
    6 条回复    2015-06-03 09:54:33 +08:00
    hanangellove
        1
    hanangellove  
       2015-06-02 15:34:13 +08:00
    你可以在didSelect方法中获取到当前点击的cell,然后把cell的属性赋给DetailController的属性
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"选中cell");
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];

    DetailViewController *detailViewController = [[DetailViewController alloc] init];
    detailViewController.xxxproperty = cell.xxxxx;
    [self.navigationController pushViewController:detailViewController animated:YES];
    }
    v2Next
        2
    v2Next  
    OP
       2015-06-02 15:41:32 +08:00
    @hanangellove 我试过不行。

    我在didSecct 方法里直接给 detailViewController.percentString = @"hello";

    然后在 DetailViewController 打印 self.percentString 都是空
    serco
        3
    serco  
       2015-06-02 15:46:09 +08:00
    @v2Next 检查自己的代码,或者完整贴出来
    black
        4
    black  
       2015-06-02 15:50:02 +08:00
    @v2Next 你在 DetailViewController 的什么方法内打印的?
    v2Next
        5
    v2Next  
    OP
       2015-06-02 15:51:29 +08:00
    @hanangellove 解决了 3Q
    hanangellove
        6
    hanangellove  
       2015-06-03 09:54:33 +08:00
    @v2Next 检查出什么问题了?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1011 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 22:15 · PVG 06:15 · LAX 15:15 · JFK 18:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.