gpt4 book ai didi

ios - 在 ios 的 tableview 中的选定单元格上设置 imageview 的问题

转载 作者:行者123 更新时间:2023-12-01 17:25:46 25 4
gpt4 key购买 nike

我有一个应用程序,我在我的 tableview 中显示一些数据,如下面的 ScreenShot1 所示。

截图 1。

enter image description here

现在我在每个 tableview 单元格上有一个 imageview。

如果我从 tableview 中选择任何单元格,我希望在该选定单元格上显示一个图像,如下面的屏幕截图 2 所示。

截图 2。

enter image description here

现在如果我选择任何其他单元格表格表格 View 。我只想在那些选定的单元格上设置我的 imageview 的图像,如下面的 Screenshot3 所示。

enter image description here

下面是我正在做的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
lblRecipe = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, 600, 32)];

lblRecipe.backgroundColor = [UIColor clearColor];

lblRecipe.font = [UIFont systemFontOfSize:20.0f];
[cell.contentView addSubview:lblRecipe];
checkedimg=[[UIImageView alloc]initWithFrame:CGRectMake(40, 20, 500, 5)];

[cell.contentView addSubview:checkedimg];
lblRecipe.text = [array objectAtIndex:indexPath.row];
return cell;

}

在 Tableview 的 didselect 方法上,我应该保持什么才能拥有这种功能?

我搜索了很多,对此有很多问题,但找不到一个。

所以请帮助我。

提前致谢。

最佳答案

你最好只继承 UITableViewCell ,因为这将让您更改单元格的绘制方式,并添加一个属性或类似的东西来指示此状态。

要创建子类,在 Xcode 中创建一个新文件,然后选择 UITableViewCell作为它的父类(super class)。

然后,在该子类中,您可以实现一个名为 setStrikeThrough: 的方法。像这样,您可以调用从表中出列的单元格:

- (void) setStrikeThrough:(BOOL) striked {
if(striked) {
// create image view here and display, if it doesn't exist
} else {
// hide image view
}
}

然后,在保存表格的 View Controller 中,您可以在 viewDidLoad 中调用它。使用表格 View 注册您的子类:
[_tableView registerClass:[YourAmazingTableCell class] forCellReuseIdentifier:@"AmazingCellSubclass"];

然后你会改变你的 tableView:cellForRowAtIndexPath:变成这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"AmazingCellSubclass";

YourAmazingTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[YourAmazingTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// here you would look up if this row should be selected
[cell setStrikeThrough:YES];

// Set up the remainder of the cell, again based on the row
cell.textLabel.text = @"chilli";

return cell;
}

关于ios - 在 ios 的 tableview 中的选定单元格上设置 imageview 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22215125/

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