繁体   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