a-two-column uitableviewcontrollerリストの作成方法は?
-
29-10-2019 - |
質問
2つの列リストでテーブルビューを表示する必要があります。ドローレクトをオーバーライドすることで、グリッドの作成に関するいくつかの関連する投稿を読みました(ここ)。ただし、IBで細胞を設計し、それをロードして各行に2つのセルを押す簡単な方法を探しています。 DrawRectを使用した例は、位置の手動設定が含まれるため、適切ではありません。自動化してこれらの2つのセルをプッシュする必要があります。出来ますか?
私は(CellForRowatIndexPathで)のようなものを探しています:
cell.contentView = emptyUIViewContainer;
[cell.contentView addSubview:FirstColumnUIView];
[cell.contentView addSubview:SecondColumnUIView];
これらの2つの列には、他のデータだけと同じ形式であるため、2つの列に2つの個別のペン先を作成する必要はありません。何か案は?
更新:直感的に、私はこのようなことをしようとしています:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the first cell.
cell1.textLabel.text = ...some text
// Configure the second cell
UITableViewCell *cell2 = [[UITableViewCell alloc] init];
cell2.textLabel.text = ...some text
//set row as container for two cells
UITableViewCell *twoColumnRowView = [[UIView alloc] init]; //initWithFrame:CGRectMake(0, 0, 200, 20)];
cell1.contentView.frame = CGRectMake(0, 0, 100, 20);
[twoColumnRowView addSubview:cell1];
cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
[twoColumnRowView addSubview:cell2];
return twoColumnRowView; // cell;
}
それは私が今遊んでいる生のプロトタイプです。ただし、コードは実行時に「無作法な例外のためにアプリを終了するためにクラッシュします。
更新2.より実用的に見えるようにコードを変更しました。奇妙ですが、数回の試行の後、私はクラッシュせずにアプリを動作させましたが、すべてのセルで奇妙な黒の背景がありました。これがコードです:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstCellIdentifier = @"FirstCellIdentifier";
static NSString *SecondCellIdentifier = @"SecondCellIdentifier";
// initialize first cell
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:FirstCellIdentifier];
if (cell1 == nil) {
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstCellIdentifier] autorelease];
}
//initialize second cell
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];
if (cell2 == nil) {
cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SecondCellIdentifier] autorelease];
}
cell1.textLabel.text = ...data
cell2.textLabel.text = ...data
UITableViewCell *twoColumnRowView = [[UITableViewCell alloc] init];
[twoColumnRowView addSubview:cell1];
//cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
[twoColumnRowView addSubview:cell2];
return twoColumnRowView; // cell;
}
私はTwoColumnrowViewを再利用していませんが、他の両方がそうしています。
解決
2つのテーブルビューを隣に配置することはできません(または少なくともそうすべきではありません)。そして、あなたがすでに知っているように、テーブルビューには1つの列しかありません。
ただし、各セルに必要なだけのデータを配置できます。
これのほとんどは、インターフェイスビルダーで行うことができます。 aでxibを作成します UITableViewCell
. 。ドラッグアンドドロップ UILabel
s正しい場所に。ラベルを見つけることでラベルを更新できます viewWithTag:
方法ですが、カスタムを作成する方が良いでしょう UITableViewCell
各ラベルを指すプロパティを備えたクラス。
テーブルはUIKITの中心であるため、Appleのドキュメントセットには多くのサンプルがあり、いくつかの優れたWWDCトークがあります。 iTunesからビデオをダウンロードできます。
他のヒント
これがあなたを助けることを願っています...
そうでなければ、最良の方法は、独立してスクロールする必要がある場合、2つのテーブルビューを並べて配置することです。
これがあなたを助けることを願っています...