2012-08-01 166 views
1

我有兩個陣列如何以編程方式創建CustomCell?

1)nameArray; 2)numberArray;

這些數組包含名稱和數字,我想在不使用nib的情況下在tableview中顯示這些名稱和數字。如何以編程方式創建自定義單元格

回答

0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *identifier = @"CellIdentifier"; 
CustomCellName *cell = (CustomCellName *)[tableView dequeueReusableCellWithIdentifier:identifier]; 
if(!cell) { 
cell = [[CustomCellName alloc] initWithStyle:UITableViewStyleDefault reusableIdentifier:identifier]; 
} 
cell.textLabel.text = [nameArray objectAtIndex:indexPath.row]; 
cell.subtextLabel.text = [numberArray objectAtIndex:indexPath.row]; 
return cell; 
} 

上面的代碼沒有經過測試的子類,我不是在電腦上做到這一點(而我的iPhone上打字出這一切),但它應該幾乎是你需要代碼明智的。

爲textLabel =屬性名大家的標籤在你的自定義單元格類設置你的標籤在你的自定義單元格類設置 subtextLabel =屬性名

希望這有助於!

0

創建一個應該是UITableViewCell的子類的新文件。在這裏你可以編寫你的可以創建2個標籤,你可以顯示你想要的地方。

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

return cell; 
} 

這裏CustomCell是的UITableViewCell