簡體   English   中英

如何將動態行和節數組傳遞給 numberOfRowsInSection 和 cellForRowAtIndexPath

[英]How To Pass Dynamic row and Section Array to numberOfRowsInSection and cellForRowAtIndexPath

我很多天都面臨這個問題。 請任何人解決這個問題。

下面是我的行數組,哪個計數應該傳入numberOfRowsInSection

    (
        (
        "Service Speed",
        "Good Service",
        "Confirmation quality",
        "Quick Service in Reservation"
    ),
        (
        "Check In",
        "Happy on their Service",
        Courtesey,
        "Quick Service at Checkin"
    ),
        (
        "Front office & reception",
        "Overall Quality of Room",
        Check,
        "Response time"
    ),
        (
        "Room Decor",
        "Time taken to serveTime taken to serveTime taken t",
        Bathroom,
        "Facilities in the Room",
        "Choice of menu",
        Housekeeping,
        "Room Service"
    ),
        (
        "Overall Comments",
        "Will you come back again"
    ),
    "Guest Satisfaction Ratings"
  )

我的問題是,如何傳遞numberOfRowsInSection行數組計數和cellForRowAtIndexPath數組值?

我嘗試使用以下代碼,但出現錯誤:

“[__NSCFString 計數]:發送到實例的無法識別的選擇器”

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [GroupName count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [GroupName objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[RowsArr objectAtIndex:section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

  cell.Label.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

}

問題出在數組“客人滿意度評級”的最后一個對象中,因為它出現在 NSString 中,所以只顯示錯誤。

你的問題在這里

[[RowsArr objectAtIndex:section] count];

這個[RowsArr objectAtIndex:section]返回一個字符串而不是數組,因此計數不能應用於字符串

您還發送RowsArr計數並訪問cellForRow sections arr

Sh_Khan 走在正確的軌道上。 你的問題出在這條線上:

[[RowsArr objectAtIndex:section] count];

您需要確定您正在使用的對象的類型,例如:

if ([[RowsArr objectAtIndex:section] isKindOfClass:[NSArray class]]) {
    return [[RowsArray objectAtIndex:section] count];
} else {
    return 1;
}

一旦你這樣做,你也會遇到一個問題

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

因為這也假設你會得到一個數組。

更改行:

cell.Label.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

為了:

cell.Label.text = [[sections objectAtIndex:indexPath.section] isKindOfClass:[NSArray class]] 
    ? [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] 
    : [sections objectAtIndex:indexPath.section];

它會處理這兩種情況。

編輯:不確定您從哪里獲取sections ,因此您可能需要將最后一個更改為:

cell.Label.text = [[RowsArr objectAtIndex:indexPath.section] isKindOfClass:[NSArray class]] 
    ? [[RowsArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] 
    : [RowsArr objectAtIndex:indexPath.section];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM