목표 c를 사용하여 다른 배열의 uitableview에서 선택한 셀 값과 선택되지 않은 셀 값을 얻는 방법

파비 트라

pls는 이미지를 확인

저는 iOS를 처음 사용하여 한 배열에서 선택한 셀 값을 얻고 목표 c에서 확인 표시를 사용하여 다른 배열에서 선택되지 않은 셀 값을 얻는 방법을 알고 있습니다.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 { 
  NSArray *selectedRows=[_mytableview indexPathsForSelectedRows];
indexofselectedcell = [[NSMutableArray alloc]init];

valueofselectedcell = [[NSMutableArray alloc]init];
for (int i=0; i<selectedRows.count; i++) {
   [indexofselectedcell addObject:indexPath
     customcell *cell = (customcell *)[_mytableview cellForRowAtIndexPath:indexPath];
    NSString *test = cell.balance.text;
    [valueofselectedcell addObject:test];
    NSLog(@"%@ the value of the selected cell value is ",valueofselectedcell);
    NSArray *array = [valueofselectedcell copy];
    NSNumber* sum = [array valueForKeyPath: @"@sum.self"];
    NSLog(@"the sum valuie is .... %@",sum);
    NSString *totalamount = [sum stringValue];
    _amountlabel.text=totalamount;
    NSInteger index = [valueofselectedcell indexOfObject:totalamount];
    NSLog(@"%ld the index value in the array is",(long)index);

}}
 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *totalamount;
if ([indexofselectedcell containsObject: indexPath])
{
    NSInteger anIndex=[indexofselectedcell indexOfObject:indexPath];
    [indexofselectedcell removeObjectAtIndex:anIndex];
    [valueofselectedcell removeObjectAtIndex:anIndex];
    NSLog(@"the value of the unselected cell value is..... %@",valueofselectedcell);
    NSArray *array = [valueofselectedcell copy];
    NSNumber* sum = [array valueForKeyPath: @"@sum.self"];
   totalamount = [sum stringValue];
    _amountlabel.text=totalamount;
    totalamount= nil;
}   }
user3182143

Pavithra 나는 귀하의 질문과 출력을 위해 샘플 프로젝트를 시도했습니다. 선택되지 않은 셀과 별도의 배열에서 셀을 선택하면 cellForRowAtIndexPath에서 두 개의 배열을 확인할 수 없어 충돌했습니다. 그래서 하나의 배열에 선택한 값과 선택되지 않은 값을 저장합니다.

이해하기 쉬운 솔루션을 만들었습니다 .Tableview의 값을 기본값으로 선택하지 않은 것으로 설정했습니다. 선택하면 (셀을 클릭 할 때) UITableViewCellAccessoryCheckmark 및 기타 표시 UITableViewCellAccessoryNone이 표시됩니다.

완벽하게 작동합니다. 쉽게 얻을 수 있습니다.

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController :  UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableViewSelectUnSelect;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController (){
    NSMutableArray *arrVal,*arrSelectedUnSelectedValues,*arrSelectedValues,*arrUnSelectedValues;
    NSMutableArray *arrSubTitle;
}

@end

@implementation ViewController

@synthesize tableViewSelectUnSelect;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    arrVal= [[NSMutableArray alloc]initWithObjects:@"India",@"China",@"Korea",@"Japan",@"Peru",@"Italy",@"France",@"US",@"Dubai",@"UK",@"Nepal",nil];
    arrSubTitle = [[NSMutableArray alloc]initWithObjects:@"100",@"70",@"80",@"90",@"50",@"60",@"70",@"50",@"40",@"30",@"80",nil];
    arrSelectedUnSelectedValues= [[NSMutableArray alloc]init];
    arrSelectedValues = [[NSMutableArray alloc]init];
    arrUnSelectedValues = [[NSMutableArray alloc]init];
    for(int j=0;j<[arrVal count];j++)
    {
        [arrSelectedUnSelectedValues addObject:@"unselected"]; //As Default it is in UnSelected State
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrVal.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strCell = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCell];
    }
    if([[arrSelectedUnSelectedValues objectAtIndex:indexPath.row] isEqualToString:@"selected"])
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = [arrVal objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [arrSubTitle objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    @try
    {
        CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewSelectUnSelect];
        NSIndexPath *indexPath = [tableViewSelectUnSelect indexPathForRowAtPoint:touchPoint];
        NSLog(@"The arrSelectedUnSelectedValues  are %@",arrSelectedUnSelectedValues );
        NSString *strValue;

        if([arrSelectedUnSelectedValues count]==0)
        {
            for(int i=0; i<[arrVal count]; i++)
            {
                [arrSelectedUnSelectedValues addObject:@"unselected"];
            }
        }
        if([[arrSelectedUnSelectedValues objectAtIndex:indexPath.row] isEqualToString:@"selected"])
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [arrSelectedUnSelectedValues replaceObjectAtIndex:indexPath.row withObject:@"unselected"];
            strValue = cell.detailTextLabel.text;
            [arrUnSelectedValues addObject:strValue];
            NSLog(@"The arrUnSelectedValues are - %@",arrUnSelectedValues);
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [arrSelectedUnSelectedValues replaceObjectAtIndex:indexPath.row withObject:@"selected"];
            strValue = cell.detailTextLabel.text;
            [arrSelectedValues addObject:strValue];
            NSLog(@"The arrSelectedValues - %@",arrSelectedValues);
        }
    }
    @catch (NSException *exception) {
        NSLog(@"The exception is-%@",exception);
    }
}

@end

먼저 응용 프로그램을 실행하면 아래와 같이 표시됩니다. 여기에 이미지 설명 입력

그런 다음 tableView에서 값을 선택하면 여기에 이미지 설명 입력

마지막으로 인쇄 된 결과는 여기에 이미지 설명 입력

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관