Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 是否可以创建一个可在多个表控制器中使用的tableViewCell?_Xcode_Uitableview - Fatal编程技术网

Xcode 是否可以创建一个可在多个表控制器中使用的tableViewCell?

Xcode 是否可以创建一个可在多个表控制器中使用的tableViewCell?,xcode,uitableview,Xcode,Uitableview,我有大约3或4个表控制器,它们都将使用同一个tableview单元格。我一直在重复第四种可能的逻辑。假设两个tableView控制器之间的信息相同,是否可以在多个tableView控制器中使用相同的tableViewCell?或者我必须为每个控制器创建一个新单元格吗?是的,您可以在多个tableView控制器中使用tableView单元格。是的,您可以在多个tableView控制器中使用tableView单元格。是的,您可以 我假设您正在使用Swift 转到文件->新建并选择CoCoTouch类

我有大约3或4个表控制器,它们都将使用同一个tableview单元格。我一直在重复第四种可能的逻辑。假设两个tableView控制器之间的信息相同,是否可以在多个tableView控制器中使用相同的tableViewCell?或者我必须为每个控制器创建一个新单元格吗?

是的,您可以在多个tableView控制器中使用tableView单元格。

是的,您可以在多个tableView控制器中使用tableView单元格。

是的,您可以

我假设您正在使用Swift

转到文件->新建并选择CoCoTouch类,如下所示

现在为自定义单元格命名类,并使其成为UITableViewCell的子类。同时选中“还创建Xib文件”框

现在,在此Xib中设计您的单元格,并在其.Swift文件中创建插座。假设您有一个自定义的tableView单元格,它看起来像这样

包含标签、图像视图或单元格中的任何内容。现在,在自定义单元格的swift文件中,您可以编写如下方法

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

return cell
}
现在,您可以在应用程序中的任何tableView中使用此单元格,如下所示

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
// do something with your cell

}
我希望有帮助

Swift 3和Swift 4的更新:

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell

    return cell
}
是的,你可以

我假设您正在使用Swift

转到文件->新建并选择CoCoTouch类,如下所示

现在为自定义单元格命名类,并使其成为UITableViewCell的子类。同时选中“还创建Xib文件”框

现在,在此Xib中设计您的单元格,并在其.Swift文件中创建插座。假设您有一个自定义的tableView单元格,它看起来像这样

包含标签、图像视图或单元格中的任何内容。现在,在自定义单元格的swift文件中,您可以编写如下方法

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

return cell
}
现在,您可以在应用程序中的任何tableView中使用此单元格,如下所示

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
// do something with your cell

}
我希望有帮助

Swift 3和Swift 4的更新:

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell

    return cell
}

非常有帮助!但是我想知道,我将如何开始连接我的IBO?我会简单地选择一个故事板单元来连接吗?@jonpeteroutlets of what?如果您正在谈论将单元格连接到tableView,则无需这样做。您只需编写上面的代码,它就会工作。如果你要向你的手机索取组件的输出,那么我也会更新我的答案。很抱歉延迟回复。我想知道是否/如何将我的单元格中的标签连接到tablecell类。好吧,只需打开自定义类的Xib并打开助理编辑器,然后像在viewController的类上创建一样创建插座非常有用!但是我想知道,我将如何开始连接我的IBO?我会简单地选择一个故事板单元来连接吗?@jonpeteroutlets of what?如果您正在谈论将单元格连接到tableView,则无需这样做。您只需编写上面的代码,它就会工作。如果你要向你的手机索取组件的输出,那么我也会更新我的答案。很抱歉延迟回复。我想知道是否/如何将我的单元格中的标签连接到tablecell类。好吧,只需打开自定义类的Xib并打开助手编辑器,然后像在viewController的类上创建一样创建插座