【问题标题】:How To Pass Dynamic row and Section Array to numberOfRowsInSection and cellForRowAtIndexPath如何将动态行和节数组传递给 numberOfRowsInSection 和 cellForRowAtIndexPath
【发布时间】:2018-02-09 12:18:56
【问题描述】:

我很多天都面临这个问题。请大家解决这个问题。

下面是我的 rows 数组,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 count]: 无法识别的选择器发送到实例"

 - (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,所以只有它显示错误。

【问题讨论】:

  • "Guest Satisfaction Ratings" 放入具有单个对象的数组中。它是由 api 服务器端而不是你纠正的。
  • 好的,当然。感谢您的建议。

标签: ios objective-c arrays iphone uitableview


【解决方案1】:

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];

【讨论】:

  • 这是问题.. sry @Evil.. 感谢您的反馈!声望低于 15 人的投票会被记录,但不会更改公开显示的帖子分数。..
  • 如果您单击答案旁边的灰色复选标记,您应该能够接受它作为答案@RohithSingh
【解决方案2】:

你的问题来了

[[RowsArr objectAtIndex:section] count];

这个[RowsArr objectAtIndex:section]返回一个字符串而不是数组,所以count不能应用于字符串

您还发送RowsArr 的计数并在cellForRow 中访问sections arr

【讨论】:

  • 你如何获得 RowsArr ,我的意思是发布它的相关初始化代码,它可能是 json 字符串数组
猜你喜欢
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多