我想在UITableView中顯示多個列。如何在UITableView中顯示多列?
例如:
的TableView
FName LName Age
----- ----- ---
Abby Michale 34
我想在UITableView中顯示多個列。如何在UITableView中顯示多列?
例如:
的TableView
FName LName Age
----- ----- ---
Abby Michale 34
您可以定義IB定製的電池,其中將包含3個標籤和功能:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = @"DetailCell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];
cell = (UITableViewCell*) [cellObjects objectAtIndex:0];
}
// setup your cell
}
當你定義在IB細胞給每個標籤的標記,所以當你需要把一個文本那麼你可以通過這樣的標籤檢索它:
label = (UILabel *)[cell viewWithTag:NAME_TAG];
label.text = myObject.name;
並用其他2個標籤重複此操作。該標籤是唯一的編號。
將這個代碼,而不是//設置你的評論
創建包含,比方說一個定製UIView
子類,三種UILabel
元素作爲子視圖。然後將單元的contentView
屬性設置爲此自定義視圖。
添加三個標籤作爲子viewsinto的UITableView細胞的content.then分配apprrpriate值,它 例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
//frame should be set accordingly(means should be framed accordingly).
UILabel *l1=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100,100)];
UILabel *l2=[[UILabel alloc]initWithFrame:CGRectMake(110,10,100,80)];
UILabel *l3=[[UILabel alloc]initWithFrame:CGRectMake(115,60,100,50)];
[cell.contentView addSubview:l1];
[cell.contentView addSubview:l2];
[cell.contentView addSubview:l3];
return cell;
}
這不是一個好approche做必要的事情看上面的回答是正確的approche做的事情做 – 2012-04-12 12:46:08
我創建UIGridView。我相信你的問題可以用同樣的技術來解決。
您可以瞭解UIGridView的源代碼。代碼非常短。
編譯它在iPhone上4 - 它真的滯後了...... – 2012-04-23 08:12:49
你有多少個細胞? – Tanin 2012-05-04 13:36:57
默認單元數 - 4列x 9行。只是編譯下載的示例項目到設備,它真的滯後。 – 2012-05-05 15:55:54
我認爲這樣做的正確方法是UICollectionView。從UITableView開始,最終由於在嘗試實現左右和上下滾動時的意外行爲而失敗。
請檢查這真棒後的完整和最新的解決方案: http://www.brightec.co.uk/blog/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns
結果會是這樣:
肯定比多標籤黑客好得多 – jjxtra 2017-05-06 18:05:55
位遲到了,但我們我們的全功能表格組件:
https://github.com/flexicious/iOSDataGrid
個一些截圖:
http://www.ioscomponents.com/Home/IOSDataGrid
隨意使用!
我創建了一個名爲「DetailCell」的自定義UIView子類,並在其上添加三個標籤。在 員工詳細信息UIView子類的tableview上添加上面的代碼。運行它,它不會在tabelView上顯示任何標籤。 – 2010-05-18 12:26:58
如果你在IB中創建它,你應該創建一個UITableViewCell,而不是UIView.See這個鏈接詳細地創建自定義單元格: http://developer.apple。COM/iphone /庫/文件/ UserExperience /概念/ TableView_iPhone/TableViewCells/TableViewCells.html – 2010-05-18 14:54:13