[英]Custom Cell look in UITableVIew
我一直在關注這個折磨:
http://www.raywenderlich.com/32283/core-graphics-tutorial-lines-rectangles-and-gradients
它涵蓋了動態表單元格的自定義,我需要使用靜態表單元格來完成。
正如他在本教程中所做的那樣,我為每個單元格賦予了標識符“ Cell”,然后我將表視圖控制器子類化並實現了這一點:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// START NEW
if (![cell.backgroundView isKindOfClass:[CostumCellBackground class]]) {
cell.backgroundView = [[CostumCellBackground alloc] init];
}
if (![cell.selectedBackgroundView isKindOfClass:[CostumCellBackground class]]) {
cell.selectedBackgroundView = [[CostumCellBackground alloc] init];
}
// END NEW
cell.textLabel.backgroundColor = [UIColor clearColor]; // NEW
return cell;
}
CostumCellBackground繪制矩形。
我收到錯誤消息:“ UITableView數據源必須從tableView:cellForRowAtIndexPath返回一個單元格:
據我了解,UITableView在情節提要中為每個單元格循環,並且應該返回單元格。
那么,這是怎么回事,為什么單元格返回nil或根本不返回?
唯一的區別是它們是靜態表,而不是原型。
如果您使用情節提要和iOS6,並且視圖控制器是UITableViewController,則如果情節提要中存在單元格標識符,則始終會得到一個單元格。 檢查cel == nil是老方法。
您確定您的情節提要中有一個帶有“單元格”標識符的自定義單元格嗎?
另外,使用此:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
如果查看UITableView.h文件,則會發現:
// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
您在哪里創建單元格?
您應該在以下位置添加:
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
這樣的代碼(或其他初始化程序):
if (cell == nil) {
cell = [[UITableViewCell alloc] init];
}
在方法開始時嘗試以下操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier
];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.