Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.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
iOS:将UITableViewCell更改为cellForRowAtIndexPath外部:_Ios_Uitableview - Fatal编程技术网

iOS:将UITableViewCell更改为cellForRowAtIndexPath外部:

iOS:将UITableViewCell更改为cellForRowAtIndexPath外部:,ios,uitableview,Ios,Uitableview,我试图在UITableView中实现一个“加载更多”单元格 如何以编程方式更改单元格“加载更多”的内容、高度等?我假设这可以通过如下所示获取所选单元格来完成。例如,当我想更改背景色时,它没有任何效果 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row == 2){ UITableViewCell *cel

我试图在UITableView中实现一个“加载更多”单元格

如何以编程方式更改单元格“加载更多”的内容、高度等?我假设这可以通过如下所示获取所选单元格来完成。例如,当我想更改背景色时,它没有任何效果

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 2){
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

}

我认为直接调用表视图委托消息不是一个好方法。。。当您必须为此“加载更多”单元视图显示一个额外的行时,您可能需要设置一个标志,并在此时调用表格视图(或插入/重新加载单个单元视图)的重新加载数据消息。

如果您要更改高度,请按以下方式执行:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(indexPath.row == 0){
            return 25;
        } else {
            return 120;         
        }
}
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[UITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
如果你想改变颜色,就这样做

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(indexPath.row == 0){
            cell.backgroundColor =  [[UIColor alloc]initWithRed:50.0/255.0 green:50.0/255.0 blue:100.0/255.0 alpha:1];
        } else {
            cell.backgroundColor = [UIColor whiteColor];
        }

//or use cell.textLabel.textColor to change the text color

}
调用此函数:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation (UITableViewRowAnimation)animation
大概是这样的:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(indexPath.row == 0){
            return 25;
        } else {
            return 120;         
        }
}
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[UITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

更多信息。

如果您的load more位于表的最底部,请将其实现为表尾。 你不会因为额外的索引而破坏你的数据源,你也不会因为flyweight而随意修改它/调整它的大小

比如: tableView.tableFooterView=FooterView

加载下一页后,如果它是最后一页,只需通过在表上将属性设置为nil来删除页脚

关于您的snippiet: -如果单元格不可见,cellForRowAtindexPath:(在表视图上)将不会返回任何内容。此外,tableView:CellForRowatineXpath(数据源版本)将撤销再次调用该单元格时可能执行的操作。
将所有单元格修改代码放入数据源中,并在需要更改时在表上调用相应的重新加载单元格方法。

您是否尝试过[tableView重新加载数据]?是的。它没有效果。此外,当我在TableView中滚动时,单元格将被重新加载,并将调用
cellforrowatinexpath:
,并且单元格的内容与以前相同。这实际上是可行的,尽管当我再次上下滚动时,单元格的更改再次消失。但这还不是问题。你知道我如何更改单元格高度吗?你的
heightforrowatinexpath
函数应该会处理这个问题。它应该使用这些数据来决定一个细胞应该有多高。里面的逻辑是什么?啊,好吧,你是对的,明白了;)-但这很棘手,因为您无法动态计算此大小。我认为Ricard的方法是最好的,因为我可以在
HeightforRowatineXpath
CellforRowatineXpath
中设置一个标志并用if语句来证明它。同意,我也会提出类似的建议,但这不是你问的问题。你能提供一些示例代码来说明如何设置这样的标志吗?我可以在
didSelectRowAtIndexPath:
中执行此操作吗?当然可以。如果您想在选择最后一个单元格时显示“load more”单元格,那么,在didSelectRowAtIndexPath上:您可以实现如下内容:如果((indexPath.row==([myDataSource nItems]-1)){self.showLoadMore=YES;[self.tableView BeginUpdate];[self.tableView InsertRowSatinExpaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(indexPath.row+1)section:(indexPath.section)]];[self.tableView endUpdates];}当标志设置为YES时,您的tableView:NumberOfRowsSection:方法应返回总计行数+1。他没有调用委托(或数据源),但不包括表。如果单元格在给定索引处可见,它将返回该单元格,并且可以随时调用。