繁体   English   中英

滚动后具有图像更改的UITableView

[英]UITableView with image change after scrolling

我正在尝试创建具有不同单元格标识符的自定义UITableView。 在第一个单元格中应显示一个图像,而在其下方应显示其余单元格。 但是,滚动后,显示的图像消失。 我试图通过寻找其他人针对类似问题但没有成功的答案来解决。

这是代码。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EventoCell *cell;
    static NSMutableString *CellIdentifier;
    if(i==0){
        CellIdentifier = @"imgCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        i++;
    }
    else{
       CellIdentifier = @"CellaEvento";
       cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

       cell.Nome.text=[[AShow objectAtIndex:indexPath.row]GetEvento];
       cell.Localita.text=[[AShow objectAtIndex:indexPath.row]GetLocalita];
       cell.Ora.text=[NSString stringWithFormat:@"%d:%d",[[AShow objectAtIndex:indexPath.row]GetOra],[[AShow objectAtIndex:indexPath.row]GetMinuti]];

       [cell setValue:[[AShow objectAtIndex:indexPath.row]GetEvento] :[[AShow objectAtIndex:indexPath.row]GetGiorno] :[[AShow objectAtIndex:indexPath.row]GetMese] :[[AShow objectAtIndex:indexPath.row]GetAnno] :[[AShow objectAtIndex:indexPath.row]GetOra] :[[AShow objectAtIndex:indexPath.row]GetMinuti]];

    }
    return cell;
}

您要在第一行显示图像吗? 如果是这样,我认为您可以更改行以判断它是否是来自

if ( i==0 )

if (indexPath.row == 0 && indexPath.section == 0)

我认为我必须是班级成员。 UITableView仅创建有限数量的UITableViewCell 通常,该数目等于显示的行数。 例如,如果屏幕只能显示10行,则UITableView创建10个单元格。 滚动后,它通过调用dequeueReusableCellWithIdentifier:forIndexPath重用创建的单元格,这使屏幕外的行释放其单元格。 向后滚动时,每个“新”输入的项目都需要一个单元格。 UITableView将为新单元tableView:cellForRowAtIndexPathtableView:cellForRowAtIndexPath 因此,作为您的方案,只能第一次显示图像行,因为在第一次之后,i甚至向后滚动也不为零。

我相信“ i”在您的代码中用作实例变量。 相反,您应该依赖作为- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法的参数传递的indexPath变量

您在表格视图中的第一项通过以下方式标识:

 indexPath.row == 0 and indexPath.section == 0

.row是给定节中的行索引。

确保正确实现了这两个委托方法,以分别正确标识节中的行数和节数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM