gpt4 book ai didi

ios - 使用自定义 Cell 在 TableView 中的任何索引处插入一行

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:14 26 4
gpt4 key购买 nike

我是 iOS 新手。我尝试了更多时间进行搜索,但结果并非我所愿。首先,我有 5 行自定义单元格。单元格仅包含文本,我想在包含一个图像的单元格的任何索引处添加一行。那么我们如何实现呢?

最佳答案

从评论中我了解到您是 UITableView 的新手。因此,请阅读一些给定的教程。您将能够在 UITableView 中添加、删除编辑单元格

UITableView tutorial
tutorial 2
Table view programming guide

像这样简单的插入

将按钮及其 Action 添加到 View 中

 -(IBAction)addNewRow:(UIButton *)sender
{
self.numberOfRows++; // update the data source
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

编辑

我想你需要这样的东西

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID1=@"CellOne";
NSString *cellID2=@"CellTwo";

UITableViewCell *cell = nil;

if (0 == indexPath.row) {

cell =[tableView dequeueReusableCellWithIdentifier:cellID1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID1];
}
cell.textLabel.text = @"New Cell";

}

else
{
cell =[tableView dequeueReusableCellWithIdentifier:cellID2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID2];
}
cell.imageView.image = [UIImage imageNamed:@"Test.jpg"];
}

return cell;
}

不要忘记增加单元格计数,即现在从 numberOfRows 返回 6。

关于ios - 使用自定义 Cell 在 TableView 中的任何索引处插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16183134/

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