1

UITableViewと同じnibファイルでカスタムUITableViewCellを設計しました。次に、次のコードを使用して表示しています。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SearchViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSInteger section = [indexPath section];

    switch (section) {
        case 0: // First cell in section 1
            return self.priceRangeCell;
            break;
        default:
            // Do something else here if a cell other than 1,2,3 or 4 is requested

            return cell;
            break;
    }

私の質問は、dequeueReusableCellWithIdentifierビジネス全体で混乱しているということです。次の部分も必要ですか??:

 static NSString *CellIdentifier = @"SearchViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

私のコードに他の問題がありますか?

ありがとう

4

1 に答える 1

0

あなたが言及したコードを絶対に使用する必要はありませんが、使用しないのは悪い形です。そのif句を使用しない場合、テーブルをスクロールするたびに、古いセルを再利用するのではなく、新しいセルを作成することになります。これは、メモリ使用量には適していません。

次に、コードに何かが欠けています。セルに何も入力していません。セルを返しますが、コンテンツを追加していません。

于 2012-08-13T15:51:02.360 に答える