【问题标题】:Fetching the checkmarked cell's label获取选中单元格的标签
【发布时间】:2013-12-06 12:28:57
【问题描述】:

我正在使用表格视图。我在第一次点击屏幕时添加了复选标记附件,并在第二次点击时删除了这个复选标记。我在表格视图的单元格上添加了一些复选标记。现在我希望具有复选标记的单元格的标签应显示在 NSlog 上。 请帮我解决这个问题。任何帮助都会非常有用。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 0)
    {
        return [celltitles count];
    }
    else
    {
    return 7;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (tableView.tag == 0)
    {
        cell.textLabel.text = [celltitles objectAtIndex:indexPath.row];

        if (indexPath.row == 0)
        {
            habitname = [[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 50)];
            habitname.placeholder = @"Habit Name";
            habitname.delegate= self;
            [cell addSubview:habitname];
        }
                else if (indexPath.row == 1)
        {
            timelbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 50)];
            timelbl.text = @"OFF";
            timelbl.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:timelbl];

            timetitlelbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
            timetitlelbl.text = @"Alert";
            timetitlelbl.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:timetitlelbl];

            toggleswitch = [[UISwitch alloc] initWithFrame:CGRectMake(250, 5, 50, 60)];
            toggleswitch.on = NO;
            [toggleswitch addTarget:self action:@selector(toggleSwitch) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
            [cell addSubview:toggleswitch];

        }
    }
    else if (tableView.tag == 1)
    {
        cell.textLabel.text = [daysarray objectAtIndex:indexPath.row];

    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.row == 1)
    {
        if (toggleswitch.on)
        {
            [self datepickershown:timelbl.text animated:YES];

            if (self.datePickerIsShowing)
            {
                //[self datepickershown];
                //[self hideDatePickerCell];
                [dscptxt resignFirstResponder];
            }
            else
            {
                [dscptxt resignFirstResponder];
                [self.timelbl resignFirstResponder];
                [self showDatePickerCell];
            }
        }
        else
        {
            timelbl.textColor = [UIColor grayColor];
        }

    }
    else if (indexPath.row > 2 && indexPath.row <10)
    {
        NSLog(@"I am Selectin the row");

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.accessoryType == UITableViewCellAccessoryNone)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;

        }
        else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
           // NSLog(@"Selected Accessory Is %d",cell.accessoryType);
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)savedata:(id)sender
{
}

上面是我正在使用的代码以及我想要日志的保存操作。

【问题讨论】:

  • 维护一个数组以保存标记为 indexpath.row 的检查,在检查新行时将索引 path.row 添加到数组中,并在取消选中该行时删除该索引 path.row来自数组。
  • 你可能应该check this answer

标签: ios iphone tableview


【解决方案1】:

不要使用UITableView 来存储您的数据。单元格被重复使用,因此这不是检测您选择了哪些项目的可靠方法。

例如,您可以将某个项目是否被选中存储在一个数组中。然后在您的cellForRowAtIndexPath 中确定是否应显示复选标记。在didSelectRowAtIndexPath 中,您更新模型(您的数组)并请求重新加载特定单元格。

这将导致更好的 MVC 分离。

编辑:添加了 UITableView 的示例

// for this example we'll be storing NSIndexPath objects in our Model
// you might want to use a @property for this
NSMutableSet *model = [NSMutableSet set];


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

    ...

    if([model containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    ... 
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ...

    // update the model
    if([model containsObject:indexPath]) {
        [model removeObject:indexPath];
    } 
    else {
        [model addObject:indexPath];
    }

    // request reload of selected cell
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade];

    ...

}

现在应该很容易记录所有选定的项目,而不必使用UITableView:现在都在您的模型中!

【讨论】:

  • 视图 (mVc) 用于显示数据,而不是存储数据。通过仅在单元格上设置复选标记,您基本上是在视图中存储数据。您不应该:将数据存储在模型 (Mvc) 中。我会用一些代码更新我的答案。
  • 好的,我得到了输出,而且似乎很好,但我没有得到想要的输出......我用这段代码得到的输出是这样的。:-" {length = 2, path = 0 - 3}", " {length = 2, path = 0 - 4}", " {length = 2, path = 0 - 5}”,“ {length = 2, path = 0 - 6}”,而我想要输出中的标签.....
  • 那是因为您可能正在记录模型数组的内容,这是一个 NSIndexPath。从单元格标题数组中获取标题。类似:for(NSIndexPath *indexPath in model) { NSLog(celltitles[indexPath.Row]); }
【解决方案2】:

做一个for循环:

NSMutableArray *mutArray = [[NSMutableArray alloc] init];

for(i = 0;i <= self.rowCount-1;i++)
 {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
           [mutArray addObject:[NSIndexPath indexPathForRow:i inSection:0]];
 }

}
NSLog(@"Array: %@", mutArray);

【讨论】:

  • 您要访问所选单元格的位置。
  • 你的意思是我想要 nslog 的操作?行数应该是多少??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多