2

チェックボックスのあるカスタムセルがあります。

すべて正常に動作しています。チェックボックスは、サブクラス化された UITableViewCell に渡す辞書に従ってチェックされます。

しかし今、チェックボックスが変更された正確なセルをテーブルビューを持つクラスに渡す必要があるので、その特定のセルの新しいチェックまたはチェック解除状態で可変辞書を設定できます。

では、これを行う方法は?、デリゲートを使用しますか?、これは問題ありませんが、問題は、チェック ボックスがどのセルで変更されたかをどのように知ることができるかということです。

4

5 に答える 5

4

このようなデリゲートを使用できます...

MyCell.h

@protocol MyCellDelegate <NSObject>

-(void)cellCheckBoxWasChanged:(MyCell *)cell;

@end

@interface MyCell : NSObject

@property (nonatomic, weak) id <MyCellDelegate> delegate;

@end

MyCell.m

@implementation MyCell

- (void)checkBoxChanged
{
    [self.delegate cellCheckBoxWasChanged:self];
}

@end

次に、できるインデックスを見つけるには...

TableViewController.m

- (void)cellCheckBoxWasChanged:(MyCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // do something to your array.
}
于 2013-05-13T10:47:38.743 に答える
1

デリゲート メソッドでも UITableViewCell を として渡してみませんかself

したがって、そのセルを使用して、インデックスパスを取得できます

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
于 2013-05-13T10:49:15.083 に答える
1

メソッド内のセル Indexpath に応じて、各チェックボックスのタグ値を設定しcellForRowAtIndexPath:ます。

   UIButton *checkboxes = customCell.checkButton
   [checkboxes setTag:indexPath.row];

次に、ボタンアクションメソッドで。

押されたボタンの正確な行を取得するために、senders.Tag 値を確認してください

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
于 2013-05-13T10:50:31.183 に答える
1

あなたはこれを行うことができます -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

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

 if([[[Usercontacts objectAtIndex:indexPath.row]objectForKey:@"isChecked"]isEqualToString:@"NO"])
        {
            [checkUncheckBtn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal];
             checkUncheckBtn.tag=1; 
        }
        else
        {
            [checkUncheckBtn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal];
              checkUncheckBtn.tag=2; 
        }
       [checkUncheckBtn addTarget:self action:@selector(checkUncheckBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

実行するとcheckUncheckBtnPressed:メソッドは次のようになります

-(void)checkUncheckBtnPressed:(id)sender
{

    UIButton *btn=(UIButton*)sender;
    UITableViewCell *cell =(UITableViewCell *) [sender superview] ;
    NSIndexPath *_indxpath = [createGroupContactsTableView indexPathForCell:cell];

    if(btn.tag==1)
    {
        [btn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal];   
        btn.tag=2;
        [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"YES" forKey:@"isChecked"];
    }
    else 
    {

        [btn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal];   
         btn.tag=1;
        [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"NO" forKey:@"isChecked"];

    }


}
于 2013-05-13T11:02:40.370 に答える
1

デリゲート パターンを使用して、Cells に checkBoxes からのイベントをリッスンさせ、それらを UITableViewController に転送させる代わりの方法を次に示します。

UITableViewController に checkBoxes からのイベントをリッスンさせ、次のコードを使用してセルの NSIndexPath を決定します。

@implementation UITableView (MyCategory)

-(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
    if([component isDescendantOfView:self] && component != self) {
        CGPoint point = [component.superview convertPoint:component.center toView:self];
        return [self indexPathForRowAtPoint:point];
    }
    else {
        return nil;
    }
}
@end
于 2013-05-13T11:02:51.517 に答える