Reputation:
I have TableViewController
and ViewController
. In viewcontroller
I have Integer (index ==1 , index==2 , index==3) index matches table row in TableViewController
. In TableViewController
I have 3 cell with image. Image in cell should show if cell selected and number index in view controller equal number row in TableViewController
. How can I do it?
ViewController.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (_index == 0) {
if (_TextField.text.length == 0) {
_TextField.layer.borderWidth = 0.5f;
_TextField.layer.borderColor = [[UIColor redColor] CGColor];
_TextField.layer.cornerRadius = 5;
_TextField.clipsToBounds = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSomethingWhenTimeIsUp:)
userInfo:nil
repeats:NO];
}
else if (_Level == 0 && [_TextField.text isEqualToString:@"1"]){
_Level = 1;
_intForString = _intForString + 1;
[self questions];}
else if (_Level == 0 && ![_TextField.text isEqualToString:@"1"]) {
_Level = 1;
_TextField.text = nil;
[self questions];
}
if (_index == 1) {
if (_TextField.text.length == 0) {
_TextField.layer.borderWidth = 0.5f;
_TextField.layer.borderColor = [[UIColor redColor] CGColor];
_TextField.layer.cornerRadius = 5;
_TextField.clipsToBounds = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSomethingWhenTimeIsUp:)
userInfo:nil
repeats:NO];
}
else if (_Level == 0 && [_TextField.text isEqualToString:@"2"]){
_Level = 1;
_intForString = _intForString + 1;
[self questions];}
else if (_Level == 0 && ![_TextField.text isEqualToString:@"2"]) {
_Level = 1;
_TextField.text = nil;
[self questions];
}
if (_index == 2) {
if (_TextField.text.length == 0) {
_TextField.layer.borderWidth = 0.5f;
_TextField.layer.borderColor = [[UIColor redColor] CGColor];
_TextField.layer.cornerRadius = 5;
_TextField.clipsToBounds = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSomethingWhenTimeIsUp:)
userInfo:nil
repeats:NO];
}
else if (_Level == 0 && [_TextField.text isEqualToString:@"3"]){
_Level = 1;
_intForString = _intForString + 1;
[self questions];}
else if (_Level == 0 && ![_TextField.text isEqualToString:@"3"]) {
_Level = 1;
_TextField.text = nil;
[self questions];
}
[[NSUserDefaults standardUserDefaults] setInteger:_intForString forKey:@"level"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setInteger: _index = 0 forKey: @"_index0"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setInteger: _index = 1 forKey: @"_index1"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setInteger: _index = 2 forKey: @"_index2"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"Cell%d", indexPath.row] forIndexPath:indexPath];
if ([self isAvailableRow: indexPath.row]){
cell.backgroundColor = [UIColor whiteGreen];
}else{
cell.backgroundColor = [UIColor colorWithGreen:0.95 alpha:1];
}
NSString *level =[[NSUserDefaults standardUserDefaults] stringForKey:@"level"];
int intForStrin =[level intValue];
NSString *level0 =[[NSUserDefaults standardUserDefaults] stringForKey:@"_index0"];
int index0 =[level0 intValue];
NSString *level1 =[[NSUserDefaults standardUserDefaults] stringForKey:@"_index1"];
int index1 =[level1 intValue];
NSString *level2 =[[NSUserDefaults standardUserDefaults] stringForKey:@"_index2"];
int index2 =[level2 intValue];
if (indexPath.row == 0 && index0 == 0 && intForStrin == 1) {
cell.imageView.image = [UIImage imageNamed:@"5.png"];
}
else if (indexPath.row == 0 && index0 == 0 && intForStrin == 3) {
cell.imageView.image = [UIImage imageNamed:@"2.png"];
}
else if (indexPath.row == 1 && index1 == 1 && intForStrin == 1) {
cell.imageView.image = [UIImage imageNamed:@"5.png"];
}
else if (indexPath.row == 1 && index1 == 1 && intForStrin == 3) {
cell.imageView.image = [UIImage imageNamed:@"2.png"];
}
return cell;
}
if I fulfills the conditions else if (indexPath.row == 1 && index1 == 1 && intForStrin == 1)
Image shown in first and second table cell. But image should appear only in the second cell.
Upvotes: 1
Views: 51
Reputation: 1458
You can create new NSMutalbeArray:
@property(nonatomic, strong) NSMutableArray *indexArray;
then add your index to this array.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (_index == 0) {
if (_TextField.text.length == 0) {
_TextField.layer.borderWidth = 0.5f;
_TextField.layer.borderColor = [[UIColor redColor] CGColor];
_TextField.layer.cornerRadius = 5;
_TextField.clipsToBounds = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSomethingWhenTimeIsUp:)
userInfo:nil
repeats:NO];
}
else if (_Level == 0 && [_TextField.text isEqualToString:@"1"]){
_Level = 1;
_intForString = _intForString + 1;
[self questions];}
else if (_Level == 0 && ![_TextField.text isEqualToString:@"1"]) {
_Level = 1;
_TextField.text = nil;
[self questions];
}
if (_index == 1) {
if (_TextField.text.length == 0) {
_TextField.layer.borderWidth = 0.5f;
_TextField.layer.borderColor = [[UIColor redColor] CGColor];
_TextField.layer.cornerRadius = 5;
_TextField.clipsToBounds = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSomethingWhenTimeIsUp:)
userInfo:nil
repeats:NO];
}
else if (_Level == 0 && [_TextField.text isEqualToString:@"2"]){
_Level = 1;
_intForString = _intForString + 1;
[self questions];}
else if (_Level == 0 && ![_TextField.text isEqualToString:@"2"]) {
_Level = 1;
_TextField.text = nil;
[self questions];
}
if (_index == 2) {
if (_TextField.text.length == 0) {
_TextField.layer.borderWidth = 0.5f;
_TextField.layer.borderColor = [[UIColor redColor] CGColor];
_TextField.layer.cornerRadius = 5;
_TextField.clipsToBounds = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSomethingWhenTimeIsUp:)
userInfo:nil
repeats:NO];
}
else if (_Level == 0 && [_TextField.text isEqualToString:@"3"]){
_Level = 1;
_intForString = _intForString + 1;
[self questions];}
else if (_Level == 0 && ![_TextField.text isEqualToString:@"3"]) {
_Level = 1;
_TextField.text = nil;
[self questions];
}
self.indexArray = [NSMutableArray new];
[self.indexArray addObject:[NSNumber numberWithInt:_intForString]];
[self.indexArray addObject:[NSNumber numberWithInt:_index]];
}
}
}
}
then in TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"Cell%d", indexPath.row] forIndexPath:indexPath];
if ([self isAvailableRow: indexPath.row]){
cell.backgroundColor = [UIColor whiteGreen];
}else{
cell.backgroundColor = [UIColor colorWithGreen:0.95 alpha:1];
}
int intForStrin =[[self.indexArray objectAtIndex:0] intValue];
int index = [[self.indexArray objectAtIndex:1] intValue]
if (indexPath.row == 0 && index == 0 && intForStrin == 1) {
cell.imageView.image = [UIImage imageNamed:@"5.png"];
}
else if (indexPath.row == 0 && index == 0 && intForStrin == 3) {
cell.imageView.image = [UIImage imageNamed:@"2.png"];
}
else if (indexPath.row == 1 && index == 1 && intForStrin == 1) {
cell.imageView.image = [UIImage imageNamed:@"5.png"];
}
else if (indexPath.row == 1 && index == 1 && intForStrin == 3) {
cell.imageView.image = [UIImage imageNamed:@"2.png"];
}
return cell;
}
Upvotes: 2