2066
13218
我列出了來自Web Service的聯繫人,並將其顯示在聯繫人“分段”的tableView中,如屏幕截圖所示。
問題是我為A部分和S部分的第一行的複選框獲得了相同的標記值。我對一個數組進行了排序並顯示在索引表視圖中。如何根據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操作:@selector(checkBoxClicked :) forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
返回單元
}
-(void)checkBoxClicked:(id)發件人
{
CGPoint buttonPosition = [發送方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!=無)
{
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 {
如果([arrayOfCharacters count] == 0)
{
返回@“”;
}
返回[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:@“ |”]];
返回到返回
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSInteger計數= 0;
for(NSString * arrayOfCharacters中的字符){
如果([character isEqualToString:title]){
返回計數;
}
數++;
}
返回0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
返回[arrayOfCharacters count];
//返回1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)節
{
//返回[mutableArray count];
返回[[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]]計數];
} 
您要為具有相同行的所有部分設置相同的標籤值。
indexPath具有兩個屬性{section,row}。
假設A部分有兩行,
對於row1-> indexPath.section = 0,indexPath.row = 0;
對於row2-> indexPath.section = 0,indexPath.row = 1;
假設S部分有兩行,
對於row1-> indexPath.section = 1,indexPath.row = 0;
對於row2-> indexPath.section = 1,對於indexPath.row = 1;
因此,對於A部分的第1行和S部分的第1行,您要設置相同的標籤值0,這就是問題所在。
嘗試如下設置標籤值。
button.tag = indexPath.section * 1000 + indexPath.row;
檢索節和行時,
NSInteger部分=(button.tag)/ 1000;
NSInteger行=(button.tag)%1000;
|
試試這個...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
靜態NSString * CellIdentifier = @“ Cell”;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
單元格= [[UITableViewCell分配] initWithStyle:UITableViewCellStyleDefault重用標識符:CellIdentifier];
}
UIButton * checkBox = [[UIButton alloc] initWithFrame:CGRectMake(280,10,50,44)];
checkBox.backgroundColor = [UIColor orangeColor];
[checkBox addTarget:self操作:@selector(checkBoxClicked:event:)forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
返回單元
}
-(void)checkBoxClicked:(id)發送者事件:(id)事件
{
NSSet * touches = [event allTouches];
UITouch * touch = [觸摸anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tv]; //這裡電視是TableView對象
NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint:currentTouchPosition];
NSLog(@“ indePath.section%d的值,indexPath.row%d”,indexPath.section,indexPath.row);
}
|
之所以發生這種情況,是因為您正在將標籤分配給各個部分的按鈕INDEPENDENT。
A和S部分的第一行都具有row =0。因此分配給它們各自按鈕的標籤為0。您應該為其分配它們,以保持對部分的引用。
我建議使用包含Section,Row的逗號分隔形式分配可訪問性提示。
而用你的方法
-(void)checkBoxClicked:(id)發件人
{
//從逗號分隔的形式中選擇字符串。第一個是您的部分,第二個是行。
}
第二個選擇是做你想做的事情,並像這樣實現Button方法。
CGPoint buttonPosition = [發送方convertPoint:CGPointZero toView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
如果(indexPath!= nil)
{
... indexpath.section是您的部分,index path.row是您的行。
}
還有第三種選擇。
在cellforRowAtIndexpath中為您的Button分配一個標題
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
[btn setTitle:<#您的行#> forState:UIControlStateDisabled];
[btn setTag <#您的部分#>];
因此,在接收您的Button方法後,您可以同時擁有Section(標籤)和Row(禁用標題)。
-(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