- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UITableView,有两个部分:免费和付费。每个部分都有不同类型的单元。空闲单元有两个标签和一个图像。付费单元有两个标签,一个图像和一个允许购买产品的按钮。购买产品后,该特定单元格上的“购买”按钮不得再次显示。也就是说,这就是单元格的初始化方式...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IDNormal = @"Normal";
static NSString *IDComplex = @"Complex";
if (indexPath.section == 0) { // show free objects
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:IDNormal];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:IDNormal] autorelease];
// product description
UILabel * labelDescription = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 54.0, 225, 18)];
[labelDescription setTextAlignment:UITextAlignmentLeft];
[labelDescription setBackgroundColor:[UIColor whiteColor ]];
[labelDescription setClipsToBounds:YES];
[labelDescription setFont:[UIFont systemFontOfSize:14.0]];
[labelDescription setTextColor:[UIColor blackColor]];
[labelDescription setAlpha:0.6];
[labelDescription setTag: 860];
[cell addSubview:labelDescription];
[labelDescription release];
// this will show the word FREE on free objects (cells)
UILabel * labelFREE = [[UILabel alloc] initWithFrame:CGRectMake(235.0, 54.0, 80, 18)];
[labelFREE setTextAlignment:UITextAlignmentCenter];
[labelFREE setBackgroundColor:[UIColor greenColor ]];
[labelFREE setClipsToBounds:YES];
[labelFREE setFont:[UIFont boldSystemFontOfSize:14.0]];
[labelFREE setTextColor:[UIColor blackColor]];
[labelFREE setAlpha:0.75];
[labelFREE setText:NSLocalizedString(@"freeKey", @"")];
[labelFREE setTag: 861];
[cell addSubview:labelFREE];
[labelFREE release];
}
cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat: @"free%d", indexPath.row] ofType:@"jpg"]];
NSString * prefixLabel = [NSString stringWithFormat: @"gratis%d", indexPath.row];
UILabel *labelDescription2 = (UILabel*)[cell viewWithTag:860];
[labelDescription2 setText:@"FREE"];
return cell;
} else { // show paid objects
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:IDComplex];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:IDComplex] autorelease];
UILabel * labelDescription = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 54.0, 225, 18)];
[labelDescription setTextAlignment:UITextAlignmentLeft];
[labelDescription setBackgroundColor:[UIColor whiteColor ]];
[labelDescription setClipsToBounds:YES];
[labelDescription setFont:[UIFont systemFontOfSize:14.0]];
[labelDescription setTextColor:[UIColor blackColor]];
[labelDescription setAlpha:0.6];
[labelDescription setTag: 1];
[cell addSubview:labelDescription];
[labelDescription release];
}
int numberPaidObject = indexPath.row + 500;
cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat: @"table-pg%d", numberPaidObject] ofType:@"jpg"]];
NSString * nomeDoProduto = [NSString stringWithFormat: @"paid%d", numberPaidObject];
if ( NotSoldProduct ) {
NSString * prefixoEtiqueta = [NSString stringWithFormat: @"paid%d", numberPaidObject];
UILabel *labelDescription2 = (UILabel*)[cell viewWithTag:1];
[labelDescription2 setText:[description objectAtIndex: numberPaidObject ];
}
return cell;
}
}
因为我必须确定单击了哪个“购买”按钮,并且我正在使用 dequeueReusableCellWithIdentifier,所以我必须使用以下方法...
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
int numeroOfPaidObject = indexPath.row + 500;
if ((indexPath.section == 1) && ObjectForSale) {
// if paid objects and object was not bought yet
// in theory this section will not be executed if the object was already bought and paid
// so, I am skipping the BUY button creation and, in theory the cell will not have a BUY
// button… the problem is that it does...
UIButton * buyButton = [[UIButton alloc] initWithFrame:CGRectMake( 235, 45, 80, 30)];
buyButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[buyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
buyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
buyButton.backgroundColor = [UIColor clearColor];
[buyButton addTarget:self action:@selector(buyNow:) forControlEvents:UIControlEventTouchDown];
[buyButton setTitle:NSLocalizedString(@"buyKey", @"") forState:UIControlStateNormal];
UIImage *newImage = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource: @"whiteButton" ofType:@"png"]]
stretchableImageWithLeftCapWidth:12.0f topCapHeight:0.0f];
[buyButton setBackgroundImage:newImage forState:UIControlStateNormal];
[buyButton setTag: numeroOfPaidObject];
[cell addSubview:buyButton];
[buyButton release];
}
}
此代码的问题是:当我重新加载表的数据时,“购买”按钮继续显示在所有单元格上,包括用户已购买的单元格。
有什么想法吗?
感谢您的帮助。
最佳答案
您可以使用调试器单步执行它,但这是我的猜测:
由于表格单元格被重复使用,最终所有已购买的商品最终都会出现在之前被未购买的商品占据的表格单元格中,因此添加了购买按钮。
如果是这种情况,您需要在代码的第二部分中添加一个 else
子句,以便在产品已被购买时从单元格中删除该按钮。
另一个问题是,每次显示单元格时,您的代码都会添加一个新按钮,因此许多/大多数单元格中可能有多个按钮。
您需要重构代码,以便在 tableView:cellForRowAtIndexPath:
中添加一次按钮,并且仅针对新创建的单元格。
有两种方法可以做到这一点:
将“复杂”单元类型分为两种类型:已购买和未购买,并分别创建/出列它们。仅将按钮添加到未购买的单元格类型。
将按钮添加到“付费”部分的所有单元格中,并在购买商品后将其隐藏。
无论哪种情况,您都需要一种方法来获取指向按钮的指针。执行此操作的首选方法是使用 tag
属性,使其成为单行:
UIButton * button = [cell viewForTag:42];
...
您还可以遍历单元格的 subview 来查找 UIButton 对象:
UIButton *button = nil;
foreach (UIView *view in cell.subviews) {
if ([view isKindOfClass:[UIButton class]])
button = (UIButton *)view;
}
我建议使用常量标记来标识按钮,然后重写您的 buyNow:
方法,如下所示:
-(IBAction)buyNow:(id)sender {
UITableViewCell *cell = (UITableViewCell *)((UIView *)sender).superview;
int itemID = [tableView indexPathForCell:cell].row + 500;
...
}
关于iphone - iOS : UITableView reloadData not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1682733/
我正在制作一个应用程序,使用 Flickr 和 Imgur 的 API 提取图像。填充存储图像 URL 和图像标题的模型后,我想重新加载 UI,以便图像填充 collectionview,但是当它被调
出于某种原因[tView reloadData] (其中 tView 是 UITableView)不会刷新我的 UITableView。 cellForRowAtIndexPath , numberO
我正在更新我的表数据资源,然后我打电话 [self.tableView reloadData] 用这个新数据加载 TableView 。这一切都发生在一个线程中。现在,它可以正常工作,假设我添加了 4
我有一个 UICollectionViewCell: “渐变 View ”是具有渐变的 UIView: 数据来源是CoreData。在 Collection View 上,我有对 CoreData 进
我是一名新手 iOS 程序员,正在开发我的第一个应用程序,但遇到了一个小问题。我想在通过网络下载一些数据后更新我的 tableView,并在其 Controller 中使用以下代码来执行此操作: -(
我的应用程序包含两个 collectionView , mainCollectionView 和 nestedCollectionView ,位于 mainCollectionViewCell 中。
我是整个 iOS 开发的新手,所以不是很擅长。我目前有一个带有两个 TableView 的 viewController。第一个表始终可见并包含类别。只有在选择第一个表中的单元格时,第二个表才应该可见
我明白我需要在调用reloadData之前更改数据源中的数据。我的问题是我不确定这是如何完成的以及为什么我的 getData 方法不会覆盖当前单元格。是否有必要为此使用 subview ?或者有没有办
我有一个 4 行的分组 tableView。我对我在两个 View 之间做事的方式进行了大修,现在 reloadData 表现得很奇怪。它只会刷新一行中的数据。 (最后一个)和其他人都没有。 我已经在
当我实例化第三个单元格时,我会将更多项目添加到我的模型数组中,然后我将更新 Collection View 数据: DispatchQueue.main.async(execute: { s
所以,我遇到了麻烦 - 无法重新加载 TableView 的数据,在命令行中我看到了良好的数据 - 当我单击按钮时 idMeet 已更改,但在模拟器中我没有看到刷新 TableView 单击按钮后,i
我正在尝试从 firebase 获取数据并传递给 tableview。 // Model import UIKit import Firebase
我有 2 个类,classA 和 classB在 classA 中,我有一个可以按需工作和刷新的 TableView 。所有的委托(delegate)和数据源都很好,还有一个属性 @property
所以看起来 Swift 中的 TableView 会根据滚动位置动态地将数据重新加载到屏幕上。但是,在我的例子中,我不希望该机制以这种方式工作,因为我的 TableView 中的项目会更新用户订单的总
所以我有一个 tableView,其中有一个标题元素和一些单元格。我正在从服务器中提取单元格,然后在完成后重新加载 tableview。好吧,这至少是现在的目标我在每次追加到数组后重新加载 tavle
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我想知道 tableView 上的 reloadData 有多贵。我想每 5 秒刷新一次表格 View 。这会导致任何性能问题吗? 最佳答案 重新加载数据将调用 TableView 数据源上的两个主要
在我的应用程序中,我有一个标签,通知用户缺少一些设置: 设置在: override func tableView(_ tableView: UITableView, cellForRowAt inde
在 UITableView 上调用 reloadData 时实际调用了哪些方法? [tableView reloadData]; 最佳答案 整个 TableView 被重新加载,它调用 cellFor
我遇到这样一种情况,代码更改其 collectionView 数据源,然后多次调用 reloadData(),我认为这会导致我的应用程序因竞争条件而崩溃,因为数据源变化非常快,所以我用 Dispatc
我是一名优秀的程序员,十分优秀!