简体   繁体   English

如何将动态行和节数组传递给 numberOfRowsInSection 和 cellForRowAtIndexPath

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

I am facing this issue from many days.我很多天都面临这个问题。 Anyone please sort-out this issue.请任何人解决这个问题。

Below is my rows array, which count should pass in numberOfRowsInSection ?下面是我的行数组,哪个计数应该传入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"
  )

My question is, how to pass row array count in numberOfRowsInSection and array values in cellForRowAtIndexPath ?我的问题是,如何传递numberOfRowsInSection行数组计数和cellForRowAtIndexPath数组值?

I tried using below code but I am getting Error:我尝试使用以下代码,但出现错误:

"[__NSCFString count]: unrecognized selector sent to instance " “[__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];

}

Problem is in the last object of array "Guest Satisfaction Ratings", Because it comes in NSString so only its showing Error.问题出在数组“客人满意度评级”的最后一个对象中,因为它出现在 NSString 中,所以只显示错误。

your problem is here你的问题在这里

[[RowsArr objectAtIndex:section] count];

This [RowsArr objectAtIndex:section] returns a string not array , so count can't be applied to a string这个[RowsArr objectAtIndex:section]返回一个字符串而不是数组,因此计数不能应用于字符串

Also you send count for RowsArr and access sections arr in cellForRow您还发送RowsArr计数并访问cellForRow sections arr

Sh_Khan is on the right track. Sh_Khan 走在正确的轨道上。 Your problem is with the line:你的问题出在这条线上:

[[RowsArr objectAtIndex:section] count];

You'll need to determine the type of the object you're getting, using, for example:您需要确定您正在使用的对象的类型,例如:

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

Once you do this, you'll also get an issue with一旦你这样做,你也会遇到一个问题

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

as this is also assuming you'll get an array.因为这也假设你会得到一个数组。

Change the line:更改行:

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

for:为了:

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

and it'll handle both cases.它会处理这两种情况。

EDIT: Not sure where you're getting sections from, so you may need to change that last one to:编辑:不确定您从哪里获取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