1

基本的に、2つのボタンが上の2行にレンダリングされたテーブルビューがあります。これは、CGRectMakeを使用して、セルサブビューにボタンを追加して行いました。テーブルビューには、スコープバー付きの検索バーもあります。

私が抱えている問題は、アプリが最初に読み込まれるときに、ボタンが希望よりも短く、設定されていることです(レンダリング時に「デフォルト」になっているように見えます)。スコープを変更すると、ボタンは設定したサイズに変更されますが、元に戻すと、ボタンは以前のサイズに戻りません。

ボタンはcellForRowAtIndexPathメソッドで作成されていますが、これがサイズの問題である可能性はわかりませんが、スコープバーの応答の問題があります(ただし、これは内部にあるすべてのものに起因する可能性が高いことを知っていcellForRowAtIndexPathますスコープが変更されるたびに呼び出されるため、アプリが自然に遅くなります。

cellForRowAtIndexPath方法は次のとおりです。

-- Edited 26/09/2012 After First Answer --

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *normalCellIdentifier = @"normalCell";
    static NSString *topRowCellIdentifier = @"topRowCell";

    UITableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier];
    if (normalCell == nil) {
        normalCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellIdentifier];
    }

    UITableViewCell *topRowCell = [tableView dequeueReusableCellWithIdentifier:topRowCellIdentifier];
    if (topRowCell == nil) {
        topRowCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topRowCellIdentifier];

        // Added in here
        button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        CGRect buttonRect;

        button1.tag = indexPath.row;
        button2.tag = indexPath.row;

        button1.titleLabel.tag = 0;
        button2.titleLabel.tag = 1;

        [button1 setTitle:@"Button 1" forState:UIControlStateNormal];
        [button2 setTitle:@"Button 2" forState:UIControlStateNormal];

        [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button2 addTarget:self action:@selector(buttonPressed2:) forControlEvents:UIControlEventTouchUpInside];

        [topRowCell addSubview:button1];
        [topRowCell addSubview:button2];

        buttonRect = CGRectMake(0.0f, 0.0f, topRowCell.frame.size.width / 2.0f, topRowCell.frame.size.height);

        button1.frame = CGRectMake(7.0f, 7.0f, buttonRect.size.width - 14.0f, buttonRect.size.height- 14.0f);
        button2.frame = CGRectMake(buttonRect.size.width + 7.0f, 7.0f, buttonRect.size.width - 14.0f, buttonRect.size.height - 14.0f);

        NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.avicode.co.uk/iphone/minepedia/plists/featured/buttons.plist"]];

        NSData *data1;
        data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button1.titleLabel.tag] objectForKey:@"button1ImageURL"]]];

        NSData *data2;
        data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button2.titleLabel.tag] objectForKey:@"button2ImageURL"]]];

        selectedIndexPathType1 = [[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button1.titleLabel.tag] objectForKey:@"type"];
        selectedIndexPathType2 = [[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button2.titleLabel.tag] objectForKey:@"type"];

        button1.imageView.image = [UIImage imageWithData:data1];
        button2.imageView.image = [UIImage imageWithData:data2];
    }

    // Configure the cell...
    if (segmentedControl.selectedSegmentIndex == 0) 
    {
        if (indexPath.section == 0) 
        {
            topRowCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return topRowCell;
        }
        else
        {
            return normalCell;
        }
    }
    else
    {
        return normalCell;
    }
}

他にご希望がありましたらお知らせください。

4

1 に答える 1

1

問題の一部は、ボタンを複数回作成してセルに追加していることだと思います。以前に作成したセルを正常にデキューしたかどうかをテストするifステートメントのthen句でボタンの作成を行う必要があります。

もう1つの問題は、セルを作成するときにインターネットからコンテンツを同期的にロードしていることです。パフォーマンス上の理由以外にない場合は、NSURLConnectionなどを使用して非同期で実行することをお勧めします。ボタンにプレースホルダー画像を使用し、インターネットから実際の画像を読み込むときにこれらを置き換えます。または、画像が2つしかないように見えるので、少なくとも、セルを作成するたびに画像をキャッシュしてロードする必要はありません。

于 2012-09-25T21:06:51.650 に答える