久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

UITableView:在混合單元格表視圖靜態(tài)和動(dòng)態(tài)單元格

UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態(tài)和動(dòng)態(tài)單元格中處理單元格選擇)
本文介紹了UITableView:在混合單元格表視圖靜態(tài)和動(dòng)態(tài)單元格中處理單元格選擇的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我正在嘗試在分組表格視圖中混合動(dòng)態(tài)和靜態(tài)單元格:我想在頂部獲得 兩個(gè)帶有靜態(tài)單元格的部分,然后是 動(dòng)態(tài)單元格部分(請參考下面的截圖).我已將表格視圖內(nèi)容設(shè)置為靜態(tài)單元格.

編輯

根據(jù) AppleFreak 的建議,我將代碼更改如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{靜態(tài) NSString *CellIdentifier = @"Cell";UITableViewCell *cell;if (indexPath.section <= 1) {//section <= 1 表示靜態(tài)單元格單元格 = [超級(jí) tableView:tableView cellForRowAtIndexPath:indexPath];} else {//部分 >1 表示動(dòng)態(tài)單元格CellIdentifier = [NSString stringWithFormat:@"section%idynamic",indexPath.section];單元格 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];}返回單元格;}

但是,我的應(yīng)用程序崩潰并顯示錯(cuò)誤消息

<塊引用>

由于未捕獲的異常而終止應(yīng)用程序'NSInternalInconsistencyException',原因:'UITableView 數(shù)據(jù)源必須從 tableView 返回一個(gè)單元格:cellForRowAtIndexPath:'

對于第 0 行和第 0 行.對于第 0 行和第 0 行,從 cell = [super tableView:tableView cellForRowAtIndexPath:indexPath] 返回的單元格為 nil.p>

我的代碼有什么問題?我的網(wǎng)點(diǎn)有什么問題嗎?我沒有設(shè)置任何插座,因?yàn)槲艺诶^承 UITableViewController 并且據(jù)說沒有任何用于設(shè)置 tableview 的插座(?).有關(guān)如何更好地做到這一點(diǎn)的任何建議?

編輯二

我在故事板中重新創(chuàng)建了我的場景(請參閱上面更新的屏幕截圖)并重寫了視圖控制器,以便從一個(gè)新的基礎(chǔ)開始.我還按照 applefreak 的建議閱讀了 Apple 論壇中的描述.但是,我使用 numberOfSectionsInTableView:tableView 方法解決了我的第一個(gè)問題,其中我將靜態(tài)部分的數(shù)量(兩個(gè))增加了一個(gè).

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {返回 [super numberOfSectionsInTableView:tableView] + 1 ;}

應(yīng)用程序崩潰并顯示錯(cuò)誤消息:

<塊引用>

由于未捕獲的異常NSRangeException"而終止應(yīng)用程序,原因:'*** -[__NSArrayI objectAtIndex:]: 索引 2 超出范圍 [0 .. 1]'

即使我遵循了 Apple 和 applefreak 的建議,為什么這段代碼對我不起作用?在 iOS 6 中 tableView 可能發(fā)生了一些變化?

解決方案:我現(xiàn)在在下面的答案中使用 AppleFreaks 代碼示例來解決這個(gè)問題.謝謝你,AppleFreak!

編輯 III:單元格選擇:

如何在混合(動(dòng)態(tài)和靜態(tài)單元格)單元格表格視圖中處理單元格選擇?什么時(shí)候調(diào)用super,什么時(shí)候調(diào)用self tableView?當(dāng)我使用

[[super tableView] selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]

并嘗試使用以下命令檢查選定的索引路徑:

UITableView *tableView = [super tableView];if ( [[tableView indexPathForSelectedRow] isEqual:customGrowthIndexPath] ) { .. }

我得到 nil 的返回值.

由于我找不到錯(cuò)誤的根源,非常感謝您的幫助

解決方案

靜態(tài)單元格需要調(diào)用 super 方法.我假設(shè)您將擴(kuò)展 UITableViewController.見下文

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{靜態(tài) NSString *CellIdentifier = @"Cell";UITableViewCell *cell;/* 根據(jù)索引路徑和您的設(shè)置檢測單元格是靜態(tài)的還是動(dòng)態(tài)的(原型) */if ("Cell 是原型")單元格 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];else if ("單元格是靜態(tài)的")單元格 = [超級(jí) tableView:tableView cellForRowAtIndexPath:indexPath];//如果需要,修改單元格屬性返回單元格;}

有關(guān)混合單元格的更多信息,請參閱混合靜態(tài)和動(dòng)態(tài)表格視圖內(nèi)容討論在蘋果論壇上.

如果您還沒有閱讀上面的蘋果鏈接,請仔細(xì)閱讀.對于動(dòng)態(tài)內(nèi)容,您將首次使用代碼本身中的給定標(biāo)識(shí)符構(gòu)建單元格.下次在病房中,您將出列單元而不是構(gòu)建它!還是老樣子.

此外,請記住靜態(tài)數(shù)據(jù)源認(rèn)為只有 2 個(gè)部分(例如).你不能問它關(guān)于第 2 節(jié)的問題,因?yàn)樗J(rèn)為只有第 0 節(jié)和第 1 節(jié).如果你要在表格末尾以外的任何地方插入動(dòng)態(tài)內(nèi)容,你需要在轉(zhuǎn)發(fā)數(shù)據(jù)時(shí)對 super 撒謊源方法.例如,您的 numberOfRowsInSection: 方法應(yīng)如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{如果(部分 == 1){返回 1;}否則如果(部分> 1){部分 - ;//基本上你正在形成動(dòng)態(tài)內(nèi)容的部分}return [super tableView:tableView numberOfRowsInSection:section];}

關(guān)鍵是對你的動(dòng)態(tài)內(nèi)容進(jìn)行調(diào)整,以便靜態(tài)數(shù)據(jù)源仍然獲得它期望的值

這是完整的工作示例并經(jīng)過測試.只需復(fù)制代碼中的所有以下方法,它應(yīng)該可以直接工作.當(dāng)您有混合單元格時(shí),您必須覆蓋所有 tableview 方法.根據(jù)您的要求返回numberOfSectionsInTableView"中靜態(tài)和動(dòng)態(tài)部分的總數(shù),然后在其余的每個(gè)方法中返回;如果有問題的部分或行是靜態(tài)的,只需調(diào)用 super,如果它是動(dòng)態(tài)的,則像在普通 UITableViewController 子類中那樣傳回相關(guān)細(xì)節(jié).在此示例中,我只是返回 nil 或硬編碼值.

如需更多信息,請查看蘋果論壇上的 此主題.

- (int)numberOfSectionsInTableView:(UITableView *)tableView{返回 3;}- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{返回零;}- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{返回零;}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{返回否;}- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{返回否;}-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{返回 44.0f;}- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{返回 44.0f;}- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{返回 44.0f;}- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{返回零;}-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{返回零;}-(int)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{返回 5;}- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{如果(部分 == 0)返回 2;如果(部分 == 1)返回 2;返回 5;}- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{if(indexPath.section <= 1)return [super tableView:tableView cellForRowAtIndexPath:indexPath];靜態(tài) NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(單元格 == 零)單元格 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];cell.textLabel.text = @"動(dòng)態(tài)行";返回單元格;}

您不會(huì)在 didSelectRowAtIndexPath 中調(diào)用 super.這是直截了當(dāng)?shù)?請參閱下面的代碼.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{int 行 = indexPath.row;[tableView deselectRowAtIndexPath:indexPath 動(dòng)畫:YES];UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];//處理單元格}

I am trying to mix dynamic and static cells in a grouped table view: I would like to get two sections with static cells at the top followed by a section of dynamic cells (please refer to the screenshot below). I have set the table view contents to static cells.

Edit

Based on AppleFreak's advice I have changed my code as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    if (indexPath.section <= 1) { // section <= 1 indicates static cells
        cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    } else { // section > 1 indicates dynamic cells
        CellIdentifier = [NSString stringWithFormat:@"section%idynamic",indexPath.section];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    }
return cell;

}

However, my app crashes with error message

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

for section 0 and row 0. The cell returned from cell = [super tableView:tableView cellForRowAtIndexPath:indexPath] for section 0 and row 0 is nil.

What is wrong with my code? Could there be any problems with my outlets? I haven't set any outlets because I am subclassing UITableViewController and supposedly do not any outlets for tableview to be set (?). Any suggestions on how to better do it?

Edit II

I have recreated my scene in storyboard (please refer to my updated screen shot above) and rewritten the view controller in order to start from a new base. I have also read the description in Apple's forum as applefreak suggested. However, I run in my first problem with the method numberOfSectionsInTableView:tableView, in which I increment the number of static sections (two) by one.

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [super numberOfSectionsInTableView:tableView] + 1 ; }

The app crashed with the error message:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

Why is this code not working for me even though I followed Apple's and applefreak recommendations? It is possible that the tableView has changed a bit in iOS 6?

Solution: I got this to work now using AppleFreaks code sample in his answer below. Thank you, AppleFreak!

Edit III: Cell Selection:

How can I handle cell selection in a mixed (dynamic and static cells) cell table view? When do I call super and when do I call self tableView? When I use

[[super tableView] selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]

and try to check for the selected index paths with:

UITableView *tableView = [super tableView];
if ( [[tableView indexPathForSelectedRow] isEqual:customGrowthIndexPath] ) { .. }

I get an return value of nil.

As I can't find the source of my error, I really would appreciate your help

解決方案

For static cells you need to call super method. I presume that you will be extending UITableViewController. See below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    /* Detect cell is static or dynamic(prototype) based on the index path and your settings */

    if ("Cell is prototype") 
       cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
   else if ("Cell is static")
       cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

  // Modify cell properties if you want

   return cell;
}

Also for more information on mixing cells see Mixing static and dynamic table view content discussion on apple forums.

[EDIT]

If you haven't read apple link above then please do so carefully. For dynamic content you will build the cell first time with the given identifier in the code itself. Next time on wards you will dequeue the cell rather than building it! It's the same old way.

Moreover, remember that the static data source thinks there are only 2 sections(for example). You can't ask it about section 2, because it thinks there are only sections 0 and 1. If you're going to be inserting dynamic content anywhere but the end of the table, you need to lie to super when you forward the data source methods. Your numberOfRowsInSection: method, for example, should look something like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 1;
    }
    else if (section > 1){
        section--; // Basically you are forming sections for dynamic content
    }

    return [super tableView:tableView numberOfRowsInSection:section];
}

The key is making the adjustment for your dynamic content so that the static data source still gets the values it expects

[EDIT]

Here is the complete working example and tested. Just copy past all of following methods in your code and it should work straight way. You have to override all the tableview methods when you have mixed cells. Return the total number of static and dynamic sections in "numberOfSectionsInTableView" as per your requirements, and then in each of the remaining methods; if the section or row in question is static just call through to super, if it's dynamic pass back the relevant details as you would in a normal UITableViewController subclass. In this example, I am simply returning nil or hard coded values.

For more information check this thread on apple forums.

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return nil;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0f;
}

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 44.0f;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(int)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 5;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
        return 2;
    if(section == 1)
        return 2;

    return 5;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section <= 1)
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];

    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    cell.textLabel.text = @"dynamic row";

    return cell;
}

[EDIT]

You don't call super in didSelectRowAtIndexPath. It's straight forward. See below code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     int row = indexPath.row;
     [tableView deselectRowAtIndexPath:indexPath animated:YES];

     UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
     //process the cell
}

這篇關(guān)于UITableView:在混合單元格表視圖靜態(tài)和動(dòng)態(tài)單元格中處理單元格選擇的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Using Instruments to test an iOS app without having source code to the application(在沒有應(yīng)用程序源代碼的情況下使用 Instruments 測試 iOS 應(yīng)用程序)
KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動(dòng)運(yùn)行/壓力測試 iOS 應(yīng)用程序以找出罕見 UI 錯(cuò)誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標(biāo)成員身份可見性)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級(jí)到 Xcode 4.5 后,iOS 5 SDK 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創(chuàng)建 UIImage 時(shí)遇到問題)
主站蜘蛛池模板: 第一福利社区1024 | 久久精品一区 | 欧美一级欧美三级在线观看 | 亚洲国产激情 | 99精品一级欧美片免费播放 | 亚洲视频中文字幕 | 麻豆精品国产91久久久久久 | 超碰97人人人人人蜜桃 | 国产在线视频在线观看 | 6080yy精品一区二区三区 | a久久 | 在线看成人av | 香蕉视频一区二区 | 国产日韩在线观看一区 | 在线日韩精品视频 | 亚洲精品日韩综合观看成人91 | 亚洲一区视频在线 | 最新中文在线视频 | 国产欧美日韩精品一区 | 完全免费av在线 | 国产精品国产精品 | 国产一区二区三区在线看 | 日韩在线免费观看视频 | 国产激情视频网址 | 午夜影晥 | 91精品国产综合久久精品 | 亚洲成人在线免费 | 亚洲aⅴ精品 | 免费一区二区 | 中文字幕精品一区 | 国产电影一区 | 在线观看你懂的网站 | 欧美精品一区在线 | 欧美精品欧美精品系列 | 日韩精品一区二区三区在线观看 | 久久国产精品一区二区 | 久久久一| 日韩亚洲欧美综合 | 男女又爽又黄视频 | 一区日韩 | 伊人一区 |