将数据从单元格加载到详细信息视图始终显示最后的数据



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

你能帮我实现这个吗,提前感谢!

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似乎是实例变量的表视图控制器,这些在cellForRowAtIndexPath中被覆盖每次显示单元格时。

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

另请注意,从服务器获取所有元素cellForRowAtIndexPath非常无效,因为此方法经常被调用。

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


添加属性

@property (strong, nonatomic) NSArray *books;

到视图控制器。在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;
    // ...
}

相关内容

  • 没有找到相关文章