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

时间:2013-10-23 18:44:12

标签: ios objective-c uitableview uitabbarcontroller

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

请你帮我解决这个问题, 提前谢谢!

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;


}

1 个答案:

答案 0 :(得分:1)

_titleString_writerString_publisherString似乎是实例变量 表视图控制器,并在cellForRowAtIndexPath中覆盖 每次显示一个单元格。

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

另请注意,在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;
    // ...
}