gpt4 book ai didi

ios - 将数据从单元格加载到 detailView 始终显示最后的数据

转载 作者:行者123 更新时间:2023-11-29 03:32:47 26 4
gpt4 key购买 nike

我有 UITableView,其中包含来自后端的一些数据,我在表中拥有所有内容,3 UILable,我将从后端获取该信息,我可以在表行中显示它们,当我单击行时,我希望在其中具有相同的标签我的详细 View ,但现在,我的所有详细 View 中只有一个信息,相同的信息,只需加载所有详细 View 的最后一行,

请您帮我完成这个实现,提前致谢!

cellForRowAtIndexPath 方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath

{

static NSString *CellIdentifier = @"BooksCell";
BooksCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[ [[NSBundle mainBundle] loadNibNamed:@"BooksCell" owner:nil options:nil]
lastObject];
}

_books = [server get_tests:10 offset:0 sort_by:0 search_for:@""];
_t = [NSMutableString stringWithFormat:@"%@ ", [_books objectAtIndex:indexPath.row]];

NSString *testdata = _t;

_components = [testdata componentsSeparatedByString:@","];
NSLog(@"_components: %@",_components);
for (NSString *aComponent in _components) {
if ([aComponent hasPrefix:@"title"]) {
_subComponents = [aComponent componentsSeparatedByString:@":"];
_titleString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"\""]];


}if ([aComponent hasPrefix:@"authors"]){
_subComponents = [aComponent componentsSeparatedByString:@":"];
_writerString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"\""]];


}if ([aComponent hasPrefix:@"name"]){
_subComponents = [aComponent componentsSeparatedByString:@":"];
_publisherString = [_subComponents[1] stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"\""]];



break;
}
}



cell.bookTitle.text = _titleString;
cell.writer.text = _writerString;
cell.publisher.text = _publisherString;

return cell;
}

didSelectRowAtIndexPath 方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 
*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];


BooksDetailView *c = [[BooksDetailView alloc] init];
[self.booksTable addSubview:c.view];

c.bDetailTitle.text = _titleString;
c.bDetailWriter.text = _writerString;
c.bDetailPublisher.text = _publisherString;


}

最佳答案

_titleString_writerString_publisherString 似乎是实例变量 TableView Controller 的,并且这些在 cellForRowAtIndexPath 中被覆盖每次显示一个单元格时。

您必须使用 didSelectRowAtIndexPath 中的 indexPath 才能获取正确的元素您的数据源。

另请注意,从 cellForRowAtIndexPath 中的服务器获取所有元素效率很低,因为这个方法被频繁调用。

您应该获取一次数据(例如在 viewDidLoad 中)并分配获取的数组到 View Controller 的实例变量或属性。然后您可以访问 cellForRowAtIndexPath 和中的数组中的元素didSelectRowAtIndexPath


添加属性

@property (strong, nonatomic) NSArray *books;

到 View Controller 。获取viewDidLoad中的数据:

- (void)viewDidLoad
{
[super viewDidLoad];
self.books = [server get_tests:10 offset:0 sort_by:0 search_for:@""];
// ... other stuff ...
}

在 cellForRowAtIndexPath 中,您只需从数组中获取元素。另外,您应该使用模型类,而不是所有字符串操作以及 Thrift API 生成的属性访问器。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = @"BooksCell";
BooksCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[[[NSBundle mainBundle] loadNibNamed:@"BooksCell" owner:nil options:nil] lastObject];
}

YourModelClass *book = self.books[indexPath.row];
cell.bookTitle.text = book.title;
// ...

return cell;
}

在 didSelectRowAtIndexPath 中也类似:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 
*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
BooksDetailView *c = [[BooksDetailView alloc] init];
[self.booksTable addSubview:c.view]; // WHY THAT?

YourModelClass *book = self.books[indexPath.row];
c.bDetailTitle.text = book.title;
// ...
}

关于ios - 将数据从单元格加载到 detailView 始终显示最后的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19549871/

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