gpt4 book ai didi

ios - 将参数从 UITableViewCell 传递到 UITableView(动态内容)

转载 作者:行者123 更新时间:2023-11-29 12:49:43 26 4
gpt4 key购买 nike

我如何通过参数将我的对象从我的自定义 UITableViewCell 类传递到我的 UITableView。因为我使用的是 Dynamic Prototype Cells,所以我无法将我的对象分配给单元格内的 TextView 。出于这个原因,我需要将我的对象从一个类传递到另一个类。

MainTableViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];


if(!error)
{
for (NSDictionary * needs in jsonArray)
{
textNeedTitle.text = [needs objectForKey: @"needTitle"];
textNeedPoster.text = [needs objectForKey: @"needPoster"];
textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
}
}

else
{
textNeedTitle.text = [NSString stringWithFormat:@"Error--%@",[error description]];
}
return cell;



// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 3;
}


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

TestTableViewCell.h

#import <UIKit/UIKit.h>

@interface TestTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextView *textNeedTitle;
@property (weak, nonatomic) IBOutlet UITextView *textNeedPoster;
@property (weak, nonatomic) IBOutlet UITextView *textNeedDescrip;

上面的每个属性都通过 Referencing Outlet 分配给它适当的 UITextView。

最佳答案

我不完全确定您要做什么,但我注意到在您的 UITableViewController 中您得到的是 UITableViewCell 而不是您的自定义类 TestTableViewCell。它应该是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = (TestTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
}

确保:* 在 Storyboard中,单元格的重用标识符是“Cell”(或更合适的名称...)* 原型(prototype)单元格的类是 TestTableViewCell

然后您应该能够访问单元格的属性,例如 cell.textNeedTitle.text = @"Something"

关于ios - 将参数从 UITableViewCell 传递到 UITableView(动态内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22702273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com