【问题标题】:How to do select all function for custom cell in UITableView in iOS如何在 iOS 的 UITableView 中为自定义单元格选择所有功能
【发布时间】:2013-10-13 08:54:42
【问题描述】:

我有一个表格,可以导入通讯簿联系人并显示图像、联系人姓名和复选框,我已经自定义了我的单元格,其中包含图像、标签和一个用作复选标记的按钮。我成功地能够显示所有联系人,并且复选标记也适用于单个单元格,但我无法实现选择所有功能,该功能会将表格中的所有按钮置于选定状态,然后再次单击它将取消选择它。这是我到目前为止所做的。

//这是我的自定义单元格的类

#import "InviteFriendsCell.h"

@implementation InviteFriendsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

       }

   //This is my checkmark button
-(IBAction)onAddTouched:(id)sender
{
    UIButton *addButton = (UIButton *)sender;
    [addButton setSelected:![addButton isSelected]];
}
@end

//这是我在FriendListViewController中的tableview显示代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"friendsCell";

    InviteFriendsCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

       if(cell==nil)
       {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"InviteFriendsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

       }
    // Configure the cell...

    NSUInteger row = 0; 
    NSUInteger sect = indexPath.section;
    for (NSUInteger i = 0; i < sect; ++ i)
        row += [self tableView:tableView numberOfRowsInSection:i];
    row += indexPath.row;

    cell.thumbImage.layer.cornerRadius=25;
    cell.thumbImage.clipsToBounds=YES;
    cell.thumbImage.image=nil;


        self.persons = CFArrayGetValueAtIndex(self.contactAdd, row);

        NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(self.persons)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        [cell.friendName setText:tweet];

        if (ABPersonHasImageData(persons))
        {
            NSData  *imgData = (__bridge NSData *) ABPersonCopyImageDataWithFormat(persons, kABPersonImageFormatThumbnail);
            cell.thumbImage.image = [UIImage imageWithData:imgData];
        }
        else
        {
            cell.thumbImage.image = [UIImage imageNamed:@"name_icon.png"];
        }

    return cell;
}

//这是我的全选按钮,应该在tableview中的所有按钮上打勾

  - (IBAction)selectAll:(UIButton *)sender {

       //This is where I need to implement the code
    }

这里的堆栈溢出也有类似的问题Need to create a select all button for UITableView and add selections to an array in iOS

但是这里的复选标记按钮是在 tableview 方法中创建的,我的情况不同,我无法实现该代码。有一个更好的方法吗?我正在使用故事板和 xcode 5,应该适用于 ios 5 或更高版本。

【问题讨论】:

  • 为什么所有的文字都写成粗体/斜体?
  • 这样看起来更好:P
  • 我如何才能对评论投反对票? :-)

标签: ios objective-c uitableview xcode5


【解决方案1】:

首先用布尔值创建一个数组

@property (retain, nonatomic) NSMutableArray *selectedArray;

根据联系人添加计数初始化您选择的数组,如果您打算扩展它,更好地对其进行概括并将其存储到 NSArray 中。这里的人是我的 NSArray

self.peopleList=(__bridge NSArray *)(self.contactAdd);

           self.selectedArray=[[NSMutableArray alloc]initWithCapacity:[self.peopleList count]];
           for(int i=0;i<[self.peopleList count];i++)
           {
               [self.selectedArray insertObject:[NSNumber numberWithBool:NO] atIndex:i];
           }

In the selectAll button action use this
- (IBAction)selectAll:(UIButton *)sender {


   for(int i=0;i<self.selectedArray.count;i++)
      [self.selectedArray replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:sender.isSelected]];
   [sender setSelected:!sender.isSelected];

   [yourTable reloadData];

}

【讨论】:

    【解决方案2】:

    您需要更改数据源和单元格。 contactAdd 需要知道联系人已被选中(您可以为此使用新数组,但为什么要这样做)。当点击按钮时,单元格需要回调到控制器以告诉控制器它是否被选中。控制器需要更新单元格以设置选中状态。

    如果您有很多单元格,您的代码当前将无法正常工作,选择一些然后滚动(其他行将显示为已选中,或者选择将丢失,具体取决于您的重用标识符是否配置正确)。

    另外,您的contactAdd 过于复杂。考虑使用多个数组(每个部分一个),因为您当前的索引维护起来很复杂。

    【讨论】:

    • 如果我像在我提供的链接中那样以编程方式在表格视图中添加按钮。行得通吗?
    • 重要的部分是你的数据模型保持选中状态并且控制器更新单元格上的按钮。控制器不需要创建按钮(但可以)。
    【解决方案3】:

    我真的认为使用预定义的“选定”方法是一种糟糕且死板的方法。所以我更喜欢以下解决方案:

    1. 只需将触摸监听器连接到每个单元格并处理 Tap 事件。当它发生时,将单元格数据源设置为“选中”并更新单元格的布局。

    2. 如果要“全选”,应将所有单元格数据源更新为“已选”,然后更新整个表格 (reloadData)

    注意:除了制作一个模板和操作数据之外,您还可以为单元格创建两个模板 - 选中和未选中的一个,并在单元格构造函数中实现自定义模板选择器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多