22570
9332
スクリーンショットに示すように、Webサービスからの連絡先のリストを作成し、連絡先の「セクション化された」tableViewに表示します。
問題は、セクションAとセクションSの最初の行のチェックボックスで同じタグ値を取得することです。1つの配列を並べ替えて、インデックス付きテーブルビューに表示しました。表示されるセクションの数に関係なく、indexPath.rowに応じて異なるタグ値を取得する方法。これが私が試したことです
cellForRowAtIndexPath内:
UIButton * checkBox;
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone)
{{
checkBox = [[UIButton alloc] initWithFrame:CGRectMake(7、8、30、30)];
}
そうしないと
{{
checkBox = [[UIButton alloc] initWithFrame:CGRectMake(15、13、30、30)];
}
// int cnt = 0;
// for(int i = indexPath.section-1; i> 0; i--)
// {
// cnt + = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:i]] count]; // arrayOfCharachtersの文字は「A」から「Z」です
//}
//checkBox.tag = cnt + indexPath.row;
[checkBox setImage:[UIImage imageNamed:@ "checkBox.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(checkBoxClicked :) forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
セルを返します。
}
-(void)checkBoxClicked:(id)sender
{{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableViewContact];
NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint:buttonPosition];
UIButton * tappedButton =(UIButton *)sender;
NSLog(@ "タグ番号=%d"、[送信者タグ]);
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@ "checkBox.png"]])
{{
[送信者setImage:[UIImage imageNamed:@ "checkBoxMarked.png"] forState:UIControlStateNormal];
if(indexPath!= Nil)
{{
NSString * finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row]; //チェックボックスIDをmutableArrayOfIdsに保存します
NSLog(@ "タグ付きチェックボタンID =%@"、finalIntId);
[arrayOfIds addObject:finalIntId];
}
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row];
}
そうしないと
{{
[送信者setImage:[UIImage imageNamed:@ "checkBox.png"] forState:UIControlStateNormal];
NSLog(@ "UnChecked");
// [arrayOfIds removeObjectAtIndex:tappedButton.tag];
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if([arrayOfCharacters count] == 0)
{{
@ ""を返す;
}
return [NSString stringWithFormat:@ "%@"、[arrayOfCharacters objectAtIndex:section]];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{{
NSArray * toBeReturned = [NSArray arrayWithArray:
[@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |# "
componentsSeparatedByString:@ "|"]];
toBeReturnedに戻る;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{{
NSInteger count = 0;
for(NSString * arrayOfCharactersの文字){
if([character isEqualToString:title]){
リターンカウント;
}
カウント++;
}
0を返します。
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{{
[arrayOfCharactersカウント]を返します。
// 1を返します;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{{
// [mutableArrayカウント]を返します;
return [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]] count];
} 
同じ行を持つすべてのセクションに同じタグ値を設定しています。
indexPathには、{section、row}という2つのプロパティがあります。
セクションAに2つの行があるとしましょう。
row1-> indexPath.section = 0、indexPath.row = 0の場合;
row2-> indexPath.section = 0、indexPath.row = 1の場合;
セクションSに2つの行があるとしましょう。
row1-> indexPath.section = 1、indexPath.row = 0の場合;
row2-> indexPath.section = 1、indexPath.row = 1の場合;
したがって、セクションAのrow1とセクションSのrow1には、同じタグ値0を設定しています。問題があります。
以下のようにタグ値を設定してみてください。
button.tag = indexPath.section * 1000 + indexPath.row;
セクションと行を取得するとき、
NSIntegerセクション=(button.tag)/ 1000;
NSInteger行=(button.tag)%1000;
|
これを試して...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{{
static NSString * CellIdentifier = @ "Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault restartIdentifier:CellIdentifier];
}
UIButton * checkBox = [[UIButton alloc] initWithFrame:CGRectMake(280、10、50、44)];
checkBox.backgroundColor = [UIColor orangeColor];
[checkBox addTarget:self action:@selector(checkBoxClicked:event:)forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
セルを返します。
}
-(void)checkBoxClicked:(id)sender event:(id)event
{{
NSSet * touches = [event allTouches];
UITouch * touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tv]; //ここでtvはTableViewオブジェクトです
NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint:currentTouchPosition];
NSLog(@ "indePath.section%d、indexPath.row%dの値"、indexPath.section、indexPath.row);
}
|
これは、セクションに依存しないボタンにタグを割り当てているために発生しています。
セクションAとSの最初の行は両方とも行= 0です。したがって、それぞれのボタンに割り当てられたタグは0です。セクションを参照したまま割り当ててください。
Section、Rowを含むコンマ区切りの形式でアクセシビリティヒントを割り当てることをお勧めします。
そしてあなたの方法で
-(void)checkBoxClicked:(id)sender
{{
//カンマ区切り形式から文字列を選択します。 1つ目はセクション、2つ目は行です。
}
2番目のオプションは、これまでに行ったことを実行し、このようにButtonメソッドを実装することです。
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if(indexPath!= nil)
{{
... indexpath.sectionはセクション、indexpath.rowは行です。
}
3番目のオプションもあります。
cellforRowAtIndexpathで、ボタンにタイトルを割り当てます
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
[btn setTitle:<#Your Row#> forState:UIControlStateDisabled];
[btn setTag <#Your Section#>];
したがって、Buttonメソッドで受信すると、セクション(タグ)と行(無効状態のタイトル)の両方を持つことができます。
-(void)checkBoxClicked:(id)sender {[button titleForState:UIControlStateDisabled]; //あなたの行
button.tag //あなたのセクション}
|
これを試して!
各セクションは、セクションの数を正しく知っている場合があり、次に個々の行の数を追加します。
[[fieldTitlesList objectAtIndex:indexPath.section-1] count] + indexPath.row
ここで、fieldTitlesListはセクションの配列です。
|
問題を解決する次のコードを追加しました
NSInteger rowNumber = 0;
for(NSInteger i = 0; i