0

我有来自后端的一些数据的 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;


}
4

1 回答 1

1

_titleString_writerString并且_publisherString似乎是表格视图控制器的实例cellForRowAtIndexPath变量,并且 每次显示单元格时都会覆盖这些变量。

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

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

您应该获取一次数据(例如 in viewDidLoad)并将获取的数组分配给视图控制器的实例变量或属性。然后您可以访问数组 incellForRowAtIndexPath和 in 中 的元素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;
    // ...
}
于 2013-10-23T18:53:22.313 回答