suj*_*jay 1 objective-c uitableview ios
我正在努力在表视图的每一行中放置一个复选框.当我点击一行时,它的值应该保存到一个数组中,在另一行的点击上,它的值同样应该保存到同一个数组,并且在同一行的点击中,该值应该从数组中删除.
请告诉我如何实现这个.我在didSelectRowAtIndexPath方法中使用以下代码,但我无法做到这一点.
if([arrData count]==0)
{
strlast = [arrName objectAtIndex:indexPath.row];
[arrData addObject:strlast];
NSLog(@"string checked in arrData %@",strlast);
}
else
{
for(int i = 0 ;i < [arrData count]; i++)
{
NSLog(@"[arrData count]:%d",[arrData count]);
strSelected = [arrName objectAtIndex:indexPath.row];
NSLog(@"strSelected:%@",strSelected);
for(int i = 0 ;i < [arrData count]; i++)
{
if([strSelected caseInsensitiveCompare:[arrData objectAtIndex:i]])
{
[arrData addObject:strSelected];
NSLog(@"arrData:%@",arrData);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
list是数组名,其中包含在tableview中查看的所有数据,用您自己的数组名替换它.假设tableArray是插入和删除值的数组.在.h文件中
NSMutableArray *tableArray;
Run Code Online (Sandbox Code Playgroud)
在.m文件中查看didload
tableArray=[[NSMutableArray alloc]init];
Run Code Online (Sandbox Code Playgroud)
tableview选择行方法: -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableArray count]>0) {
if ([tableArray containsObject:[list objectAtIndex:indexPath.row]]) {
[tableArray removeObject:[list objectAtIndex:indexPath.row]];
NSLog(@"data removed");
NSLog(@"tableArray%@",tableArray);
}
else {
[tableArray addObject:[list objectAtIndex:indexPath.row]];
NSLog(@"data added");
NSLog(@"tableArray%@",tableArray);
}
}
else {
//[tableArray addObject:[NSString stringWithFormat:@"%d",indexPath.row]];
[tableArray addObject:[list objectAtIndex:indexPath.row]];
NSLog(@"data added");
NSLog(@"tableArray%@",tableArray);
}
}
Run Code Online (Sandbox Code Playgroud)
在dealloc中释放数组我测试了代码我希望它可以帮助你....
| 归档时间: |
|
| 查看次数: |
218 次 |
| 最近记录: |