首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法添加UITextViewCell的accessoryType

无法添加UITextViewCell的accessoryType
EN

Stack Overflow用户
提问于 2015-03-08 21:27:40
回答 1查看 158关注 0票数 0

我需要获得UITableView单元格的可寻址性,以便如果用户先前选择了该行,则可以设置复选标记。最初,用户检查tableViewCell,我将其移动到textField并将其存储在Core中。现在,如果用户想要更改UITableView中的某些内容,我希望能够显示已经检查或未选中的内容。

因此,我有一个UITableView,它出现在UIPopover中(-cellForRowAtIndexPath之外);如果一个特定单元格的文本值等于NSArray中的一个元素,我希望将单元格的accessoryType设置为checked。这是我的更新的代码:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

//  get the timeFormat
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:@"preferencesDictionary"] mutableCopy];
int iTimeFormat = [[preferencesDict objectForKey:@"timeFormat"] intValue];  //  set timeFormat

if(tableView.tag == kServicesTableView) {  //  services array

    static NSString *CellIdentifier = @"servicesCell";  //  servicesCell is just an identifier; not used that I can tell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier];

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

    // Configure the cell
    SingletonServicesArray *sharedServicesArray = [SingletonServicesArray sharedServicesArray];  //  initialize

    [cell.textLabel setText:[sharedServicesArray.globalServicesArray objectAtIndex:indexPath.row]];

    if( ![soServices hasText] )  {  //  no text in text box
        for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];

            //  if row has been checked, remove the check
            UITableViewCell *cell = [servicesTableView cellForRowAtIndexPath:path];
            if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
                cell.accessoryType = UITableViewCellAccessoryNone;
                 break;
            }
        }
    }
    else  {  //  not empty
        for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
            UITableViewCell *cell = [servicesTableView cellForRowAtIndexPath:path];

            //  if row is in soServices textfield, add a checkmark
            if ([soServices.text containsString: sharedServicesArray.globalServicesArray[i]])  {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                break;
            }
        }
    }

    return cell;

}

它不起作用;我知道至少有两个服务在数组中,所以我应该有两个带有检查点的单元格;我得到了zilch!我怎样才能让它正常工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-19 11:48:19

如果我正确的话,这个cellForRowAtIndexPath正在更新一个不同的tableView的单元格。将上述代码添加到需要更新的cellForRowAtIndexPath的tableView中将更符合逻辑。

如果您试图更新该tableView的单元格,则以下代码将无法工作:

代码语言:javascript
运行
复制
for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [servicesTableView cellForRowAtIndexPath:path];

        //  if row is in soServices textfield, add a checkmark
        if ([soServices.text containsString: sharedServicesArray.globalServicesArray[i]])  {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
        }
    }

相反,你可以写:

代码语言:javascript
运行
复制
for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) {
    //  if row is in soServices textfield, add a checkmark
    if ([soServices.text containsString: sharedServicesArray.globalServicesArray[i]])  {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        break;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28932106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档