-2

カスタムセルでテーブルビューを作成しようとしました。いくつかのチュートリアルを見ましたが、新しいプロジェクトを開く代わりに、ステップバイステップに従ってください。今のやつでやってみた。そうすれば、問題を解決し、さらに学ぶことができます。

これまでに行った手順は次のとおりです。単純なテーブルビュー:

ここに画像の説明を入力

(WorkoutList.xib)

WorkoutList.h (workoutTableView は上の図で見たものです)

ここに画像の説明を入力

WorkoutList.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.workoutTableView.dataSource = self;
    self.workoutTableView.delegate = self;

    TitleArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return TitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

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

    WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.workoutTitle.text = [TitleArray objectAtIndex:indexPath.row];
    cell.workoutImage.image = [UIImage imageNamed:@"list-item-icon3"];

    return cell;
}

WorkoutCell.xib:

ここに画像の説明を入力

カスタム クラス: WorkoutCell 識別子: Cell

WorkoutCell.h:

ここに画像の説明を入力

私が見るのは空のTableViewだけです..

長い質問であることは承知していますが、それを理解し、自分の間違いがどこにあるかを確認することは、私にとって本当に重要です。どうもありがとう !

4

3 に答える 3

0

ここでは、.h および .m ファイルを作成する必要はありません..WorkoutCell.xib ファイルを作成するだけです..次に、File Inspecter(RHS) で UIImageview(tag 1) および UILabel(tag2) のタグ値を設定します。次に、次のコードを記述します。 cellForRowAtIndexPath で

        static NSString *MyIdentifier = @"WorkoutCellIdentifier";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

            // If no cell is available, create a new one using the given identifier.
            if (cell == nil) {
                // Use the default cell style.
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkoutCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }

            UIImageView *img = (UIImageView *) [cell viewWithTag:1];
            [img setImage:[UIImage imageNamed:@"your image"]];
            UILabel *lbl=(UILabel *)[cell viewWithTag:2];
            [lbl setText:@"your Text"];
            }
     }
于 2013-10-31T10:10:26.437 に答える