我想在tableView中向不同的单元格添加不同的图像,其中我已经有了一个字符串列表,这是我的代码,类别的结构:
struct QCategoryy {
var name:String
var image:UIImage
var isSelected = false
init(name:String, image.UIImage) {
self.name = name
self.image = image
}
extension QCategoryy: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.name = value
}
init(unicodeScalarLiteral value: String) {
self.init(name: value)
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(name: value)
}
}
这里是我创建列表的地方(我将添加到tableView中)
import Foundation
import UIKit
import CoreLocation
import Alamofire
class NearbyPlaces {
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = ["Art_gallery", "Amusement_park", "Zoo", "Bar", "Night_club", "Movie_theater", "Restaurant", "Shopping_mall", "Atm", "Gym", "Store", "Spa", "Museum", "Stadium", "Hardware_store", "Casino", "Library", "Painter", "Book_store", "Bowling_alley", "Cafe", "Clothing_store", ]
return list
}
对于列表中的每个项目,我想添加单元格大小的特定图像,但我该怎么办?
修改
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "CATEGORY_CELL"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
/* cell.accessoryType = rowIsSelected ? .checkmark : .none */
cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
cell.textLabel?.text = list[indexPath.row].name
return cell
}
答案 0 :(得分:0)
列表是静态的,对吗?
为什么不向对象添加图片网址(或您需要的图片网址)。这将解决你的问题^^。所以你可以在单元格中调用它来行:)
答案 1 :(得分:0)
public class ViewData extends AppCompatActivity {
DatabaseHelper mDb;
TextView EList;
Button mBtn;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
listView = (ListView) findViewById(R.id.listView);
mBtn = (Button) findViewById(R.id.buttonList);
mDb=new DatabaseHelper(this);
view1All();
}
public void view1All() {
mBtn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> theList=new ArrayList<>();
Cursor res = mDb.getAllData();
if(res.getCount() == 0) {
// show message
// showMessage("Error","Nothing found");
// return;
}
while (res.moveToNext()) {
theList.add(res.getString(1));
ListAdapter listAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
);
}
}
您无法在评论^^
中阅读答案 2 :(得分:0)
正如@TMX所说,你可以使用:
func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
请参阅:https://developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow
并且:https://stackoverflow.com/a/8079765/7510062
如果您刚开始编写代码,则应遵循本教程:https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html
..为了理解它的工作原理,它应该很容易!
答案 3 :(得分:0)
如果您可以创建自定义单元格会更好。而是在CellForRowAtIndex方法中使用相同的代码。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "CATEGORY_CELL"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
cell.textLabel?.text = list[indexPath.row].name
cell.imageView?.image = list[indexPath.row].image // make sure you are saving images in struct.
return cell
}
初始化为
init(name:String, image:UIImage) {
self.name = name
self.image = image
}
将func更改为
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "name", image: UIImage(named: "imageName")!)]
return list
}
扩展代码:
extension QCategoryy: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.name = value
self.image = UIImage()
}
init(unicodeScalarLiteral value: String) {
self.init(name: value, image: UIImage())
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(name: value, image: UIImage())
}
}