儘管這是最常問的問題之一,但我找不到一個全面的答案。我需要在UITableView中定製單元格。一些包含標籤或文本字段,一些包含圖像和按鈕。我已經爲每種類型的單元製作了單獨的類。我正在使用具有多個部分的GroupStyle表。現在我將與開關的情況下cellForIndexPath細胞部分,如果其他的行節:在UITableView中添加多個自定義單元格
id cell;
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
CellA *cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
else if(indexPath.row==1) {
CellB *cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
break;
case 1:
if(indexPath.row==0) {
CellC *cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
break;
default:
break;
}
return cell;
我必須在最後返回單元,以及因爲由於細胞的定義裏面的代碼塊,細胞變得無法辨認。爲了解決這個問題,我在上面聲明瞭帶有id的cell。但是我知道這不是正確的方法。我該如何解決多種類型單元格的聲明和訪問問題?
現在有4-5行適合一個屏幕,不需要滾動。所以,我不重複使用單元格。但編輯時會擠入更多的行。而在另一個表格中,還有更多行可以滾動屏幕。這意味着我必須重用單元格。所以,我的問題的第二部分是;我如何重用多個自定義單元格?
你並不真正需要的switch-case – Markinson 2015-07-02 23:27:48