0

バックエンドからのデータを含むUITableViewがあり、すべてがテーブルにあり、3 UILableであり、バックエンドからその情報を取得し、それらをテーブル行に表示できます。行をクリックすると、詳細ビ​​ューに同じラベルが必要です、しかし今、すべての詳細ビューに同じ情報が 1 つだけあり、すべての詳細ビューの最後の行をロードするだけです。

この実装で私を助けてください、事前に感謝します!

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およびTable View Controllerのインスタンス変数の_publisherStringようで、これらは セルが表示されるたびに上書きされます。cellForRowAtIndexPath

データ ソースの正しい要素を取得するには、 indexPathinを使用する必要があります。didSelectRowAtIndexPath

また、このメソッドは頻繁に呼び出されるため、サーバーからすべての要素をフェッチすることcellForRowAtIndexPath は非常に効果的ではないことに注意してください。

データを 1 回 (たとえば で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;
    // ...
}
于 2013-10-23T18:53:22.313 に答える