0

私のアプリにはサーバーからのリストがあり、スクロールがダウンするたびに新しいデータのリクエストを送信してリロードします。セルを10個受け取りました。これはうまくいきます。

しかし、問題は、たとえば1つのセルで値を変更すると、セル8、15などで値が変更されることです。

データは、サーバーから情報を取得してセルに表示するクラスです。getDataAt は、値をセルに入れるセル クラスのメソッドです。

numberOfRowsInSection では、リストが完全かどうかを確認すると、テーブル ビューに追加のセルが表示され、さらに行が読み込まれます。リストが不完全な場合は、表示する行がこれ以上ないことを意味し、このセルは表示されません。

コード :

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

    if (indexPath.row < [self.list size])
     {

        static NSString *cellIdentifier = @"CellIdentifier";

        self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

        Data* data = [self.list getDataAt:indexPath.row];

        NSLog(@"%@", indexPath);
        cell.delegate = self;
        [cell setCell:data];


        return  _cell;

    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {

return [self.list size] + (self.list.complete ? 0 : 1);

}

私がこれまでに見つけたのは、上下にスクロールすると indexPath が混乱し、さらに 10 個のセルを追加すると、前のセルの indexPath.row が必要になることです。

4

3 に答える 3

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {   
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)  
   {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row < [self.list size]) 
     {
        Data* data = [self.list getDataAt:indexPath.row];
        NSLog(@"%@", indexPath);
        [cell setCell:data];
     }

   return cell;
}
于 2013-03-14T12:54:21.497 に答える
0

主な問題は、セルが効率的に割り当てられていないことです。あなたはあなた自身の方法として以下を実装する必要があります:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier] autorelease];
    [self setCell:cell atIndex:indexPath.row];
    }
    else{
    [self reuseCell:cell atIndex:indexPath.row];
    }
    return cell;
}

setCell:atIndex: メソッドは次のようになります。

setCell:(UITableViewCell*)cell atIndex:(int)index{
Data* data = [self.list getDataAt:index];
[cell setCell:data];
}

セルにラベルがあるとします。setCell のセルにラベルを追加しています。

(void)setCell:(Data*)data{
label.text = @"text from data";
[cell addSubView:label];//this is the important part, you are adding the label on cell as a subview
//doing other stuff
}

これで、reuseCell メソッドは次のようになります (セルにラベルがあると仮定すると、今回はセルにラベルを割り当てたり追加したりするのではなく、ラベルをリセットする必要があります)。

reuseCell:(UITableViewCell*)cell atIndex:(int)index{
Data* data = [self.list getDataAt:index];
cell.label.text = @"text from data";//here your are modifying the label, not adding the label as a subView of the cell
//doing other stuff
}

最後に、「indexPath.row < [self.list size]」をチェックする必要はありません。「- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)セクション"。

「setCell:」メソッドの詳細と、セルをプロパティとして使用する理由を提供していただければ、より明確に説明できます。

于 2013-04-29T07:31:56.787 に答える
0

ほとんどの場合、行インデックスが範囲外であることを確認する必要はありません。代わりに、numberOfRowsInSection メソッドで適切な値を返す必要があります。(indexPath.row < [self.list size]) が間違っている場合、何を返しますか? nil を返すと、クラッシュします。

また、ここではエラーである可能性があります: セルへのアクセスに異なる変数を使用しています: 'cell' と '_cell'。また、最後のセルをローカル変数に保存する必要はありません。

再利用可能なセルのクラス (またはペン先) を以前にテーブル ビューに登録したと仮定します。

次のようなことを試してください:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CellIdentifier";

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    Data* data = [self.list getDataAt:indexPath.row];
    [cell setCell:data];
    return  cell;

}

– tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list size];
}
于 2013-03-14T14:11:35.040 に答える