gpt4 book ai didi

ios - 如何在单元格中显示图像?

转载 作者:行者123 更新时间:2023-11-29 10:23:03 25 4
gpt4 key购买 nike

我有 TableViewControllerViewController。在 viewcontroller 中,我有 Integer (index ==1 , index==2 , index==3) 索引匹配 TableViewController 中的表行。在 TableViewController 中,我有 3 个带有图像的单元格。单元格中的图像应显示所选单元格和 View Controller 中的编号索引是否等于 TableViewController 中的行号。我该怎么做?

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;
}

如果我满足条件 else if (indexPath.row == 1 && index1 == 1 && intForStrin == 1) 图片显示在第一个和第二个表格单元格中。但是图像应该只出现在第二个单元格中。

最佳答案

您可以创建新的 NSMutalbeArray:

@property(nonatomic, strong) NSMutableArray *indexArray;

然后将您的索引添加到此数组。

- (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]];
}
}
}
}

然后在 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;
}

关于ios - 如何在单元格中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33877297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com