带有复选标记和详细信息披露的表格视图

Posted

技术标签:

【中文标题】带有复选标记和详细信息披露的表格视图【英文标题】:tableview with checkmark and detail disclosure together 【发布时间】:2013-02-23 04:54:53 【问题描述】:

我正在制作一个带有多行选择选项的表格视图。因此,我使用了复选标记附件类型操作。我还需要编辑/重命名所选行中的文本。

基本上,我需要在单元格的左侧打上复选标记(复选框),在右侧的单元格打上细节披露,两者都是功能性的。

下面的代码是我有的复选标记,目前复选标记出现在单元格的右侧。

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

    // Navigation logic may go here. Create and push another view controller.

    TSIPAppDelegate *appDelegate = (TSIPAppDelegate *)[[UIApplication sharedApplication]delegate];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    if (cell.accessoryType==UITableViewCellAccessoryNone)
    
      cell.accessoryType=UITableViewCellAccessoryCheckmark;
      appDelegate.selectedFile = cellText;
      if (prevSP!=indexPath.row) 
        cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prevSP inSection:0]];
        cell.accessoryType=UITableViewCellAccessoryNone;
        prevSP=indexPath.row;
      
    
    else if (prevSP!=indexPath.row)
      cell.accessoryType=UITableViewCellAccessoryNone;
    

有什么建议吗?

When a row selected, checkmark should be enabled/disabled AND disclosure button selected, it should open a new view for editing the selected row.

【问题讨论】:

您能附上您的代码吗?这将使其他人更好地了解您目前拥有的东西。 @Jesse:我已经添加了代码。请检查。详情披露,待写。 @Arun 更好的方法是代替 UITableViewCellAccessoryCheckmark,您可以添加一个 UIImagview,它在每个单元格的开头和附件类型作为每个单元格的末尾都有复选标记图像 【参考方案1】:

accessoryType 类型是枚举 UITableViewCellAccessoryType,根据定义它不会接受多个值,因为它不是按位枚举。因此,您必须选择一个并通过自定义代码模仿另一个。

【讨论】:

【参考方案2】:

我建议在 tableview 上使用 UITableViewCellAccessoryCheckmark 附件类型并向单元格添加“详细信息披露”按钮。不幸的是,您不能完全按照您的要求做,但这是我发现的最干净的替代方法。

【讨论】:

【参考方案3】:

这是我在我的一个应用程序中使用的示例代码

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.textLabel.text=[NSString stringWithFormat:@"%@",[cellarray objectAtIndex:indexPath.row]];
    cell.textLabel.textColor=[UIColor blackColor] ;
    cell.textLabel.tag=indexPath.row;
    cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    // cell.textLabel.highlightedTextColor=[UIColor colorWithRed:242.0f/255.0f green:104.0f/255.0f blue:42.0f/255.0f alpha:1] ;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;




UIImageView *ima=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tick.png"]];
ima.frame=CGRectMake(280, 15, 14, 14);
ima.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;

int row = [indexPath row];
//cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
cell.textLabel.textColor= (row == selectedRow) ? [UIColor colorWithRed:242.0f/255.0f green:104.0f/255.0f blue:42.0f/255.0f alpha:1] : [UIColor blackColor] ;
if (row==selectedRow) 

    [cell.contentView addSubview:ima];



UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
[tempImageView setFrame:tableView.frame];

tableView.backgroundView = tempImageView;
[tempImageView release];
return cell;



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

selectedRow = [indexPath row]; // selected row is of type int  declared in .h

[tableView reloadData];



此代码将在整个 tableView 中只有一个复选标记。您可以将其修改为在其中包含多个复选标记

希望对你有帮助!!!

【讨论】:

看来,您正在该行的子视图中添加图像。但是,我需要的是,将选中标记的行存储在一个数组中。并允许以某种方式对所选行的文本进行“重命名/编辑”。是否可以“重命名”文本? @Arun 是的,您告诉过您需要在左侧进行复选标记并将附件类型设置为右侧。这是其中一种方式。您可以将 textlabel 文本存储在数组中,以便在 didSelectRowAtIndexPath 中重命名 我只想为“重命名”目的进行详细披露。启用“重命名/编辑”选项比保持左对齐或右对齐具有更高的优先级。谢谢。 @Arun 如果我理解正确。在触摸细节披露时,您需要重命名单元格文本标签。并且详细披露必须是复选标记类型吗? 触摸行中的任意位置,应启用或禁用复选标记。触摸披露标记应该允许编辑文本。

以上是关于带有复选标记和详细信息披露的表格视图的主要内容,如果未能解决你的问题,请参考以下文章

如何根据按下的按钮取消隐藏标记的详细信息披露按钮

iOS UITableViewCell 配件在左侧?

在没有自定义单元格的情况下向 uitableview 显示详细信息披露按钮

指定“左侧详细信息”表格视图单元格的文本

从详细信息披露按钮 segue 传递对象

如何使用“详细信息披露”按钮?