2013-06-28 44 views
1

我有一個UITableView部分。該部分中的所有單元格都具有從UITableViewCell的子類派生的名爲PLContactCell的單元。具有不同格式和行爲的UITableView單元格在同一表部分

我想要做的是,對於表格的最後一行,不使用PLContactCell。我只想使用我可以格式化的普通UITableViewCell,但是我想要。我也希望能夠讓這個單元格不響應被竊聽。

我最初的cellForRowAtIndexPath方法是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

PLContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[PLContactCell reuseIdentifier]]; 

    if (!cell) { 
     cell = [PLContactCell reusableCell]; 
     cell.delegate = self; 
    } 

    id modelObject = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

    if ([modelObject isKindOfClass:[NSString class]]) { 
    [cell configureWithString:modelObject]; 
    } else { 
     [cell configureWithUser:modelObject]; 
    } 

    return cell; 
} 

EDIT 所以,我試圖創建的XIB文件一個UITableView細胞,並添加「newCell」的重用標識符給它。然後我添加了以下代碼:

if (indexPath.row == [[sections objectAtIndex:indexPath.section] count] - 1) { 
     NSString *CellIdentifier = @"newCell"; 
     noFormatCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
} 

這什麼都不做。我的問題是,如何訪問該部分的最後一行,以及如何使它不是PLContactCell而是UITableView Cell。

+0

您有具體問題嗎? –

+0

也許在'cellForRowAtIndexPath:'你可以比較(indexPath.row + 1)到[tableView numberOfRowInSection:indexPath.section]的開頭,所以你知道你是否在最後一個單元格。如果是這樣,請致電另一個單元。但考慮Derek答案似乎是你需要的 – zbMax

+0

@TimothyMoose:我的問題是,我如何訪問該部分的最後一行,以及如何使它不是一個PLContactCell,而是一個UITableView單元格。 – jac300

回答

0

如果它總是在最後,你可能會考慮使用UITableView的頁腳視圖。然後,您可以在UITableViewDataSource中保留一些額外的邏輯。

如果它必須作爲一個單元格,那麼您必須在最後一個部分添加額外的行數,然後在-tableView:cellForRowAtIndexPath:實現中執行if語句檢查以注意它。我強烈建議你嘗試頁腳方法,因爲它更清晰,更容易從現在開始算出幾個月/幾年後你在做什麼。

這是一些代碼。請注意,如果您在UITableView中使用分組樣式,則需要創建另一節。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == sections.count - 1) //Looking for last section 
    { 
     return [sections objectAtIndex:section].count + 1; //Add one to the last section 
    } 

    return [sections objectAtIndex:section].count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSInteger row = indexPath.row; 


    if ((sections.count == indexPath.section) && [sections objectAtIndex:indexPath.section].count == indexPath.row) 
    { 
     //Here you would create and return your UITableViewCell 
    } 

PLContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[PLContactCell reuseIdentifier]]; 

    if (!cell) { 
     cell = [PLContactCell reusableCell]; 
     cell.delegate = self; 
    } 

    id modelObject = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

    if ([modelObject isKindOfClass:[NSString class]]) { 
    [cell configureWithString:modelObject]; 
    } else { 
     [cell configureWithUser:modelObject]; 
    } 

    return cell; 
} 
+1

是的,我寧願做頁腳,但我不負責該項目,顯然它需要成爲一個單元格。現在我有:if(indexPath.row == [[sections objectAtIndex:indexPath.section] count] - 1){0} {0} {0} {NSString * CellIdentifier = @「newCell」; noFormatCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; }但是這並沒有做任何事情。我使用該單元格標識符在筆尖中創建了一個單元格 - 我不確定如何對其進行編碼。 – jac300

+0

上面的一些代碼。 – Derek

相關問題