我正在尝试实现UICollectionView和显示图像.我正在使用SDWebimage哪个在tableviewcells中完美运行但是当我尝试使用它时UICollectionviewCell它不会停止并删除activityindicator.如果没有下载的图像,它会放置占位符图像.我不确定tableviewcell和collectionviewcell之间可能导致此问题的区别.
这是代码:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"personImageCell";
PersonCollectionViewCell *cell = (PersonCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Person *person = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *imgURL=[person.imageurl stringByAppendingString:@"?maxheight=300&maxwidth=400"];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
activityIndicator.center = CGPointMake(cell.ivPersonImage.frame.size.width /2, cell.ivPersonImage.frame.size.height/2);
[cell.ivPersonImage setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached){
[activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
NSLog(@"activity indicator should be removed");
}failure:^(NSError *error){
[activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
cell.ivPersonImage.image = [UIImage imageNamed:@"placeholder.png"];
}]; …Run Code Online (Sandbox Code Playgroud) 我有一个带有UIViewController的应用程序,里面有一个UICollectionView IBOutlet.

UICollectionView中的单元格是MyCustomCell,并且此方法设置其UILabel:
-(void)setCellLabel:(NSString *)value{
NSLog(@"settinglabel");
cellLabel.text = @"hardcode"; //added for testing purposes
}
Run Code Online (Sandbox Code Playgroud)
单元格具有属性类型类型,在故事板中将其标识为MyCustomCell,以及它的出列标识符.UIViewController采用数据源和委托协议.IBOutlet UILabel的cellBabel插座连接到故事板.方法定义为:
- (void)viewDidLoad{
[super viewDidLoad];
self.restNames = @[@"Orange",@"Naranja",@"Narnia"];
[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"MyCustomCellID"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"%d",[self.restNames count]);
return [self.restNames count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
[cell setCellLabel:@"hi"];
NSLog(@"fitsname %@",[self.restNames objectAtIndex:indexPath.row]);
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我在tableview中绘制了三个单元格,对应于我的数组中的3个对象.该数组只包含我直接由objectAtIndexPath设置的字符串对象,但我决定将它直接设置为@"hi",因为它不起作用.我甚至将setCellLabel方法中使用的值更改为硬编码值,并且每个单元格中只保留"Label"默认字符串.
为什么cellLabel没有正确设置?
在ImageView中减少图像大小有问题.在CollectionViewCell中有ImageView的约束:两个水平和两个垂直空间.
第一个屏幕是iOS7,第二个屏幕是iOS8.

ImageURL它是通过URL加载图像的自定义类,它工作正常,也设置了图像
_clothesImageView.image = [UIImage imageNamed:@"imageName.png"];
- (void)configCellWithClothesModel:(ClothesModel *)clothesModel
{
[_clothesImageView setImageURL:[NSURL URLWithString:clothesModel.imageURL]];
}
Run Code Online (Sandbox Code Playgroud)
ClothesModel:
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self)
{
_isShop = @(YES);
self.title = [self checkNullAndNill:dict[kTitle]];
self.info = [self checkNullAndNill:dict[kDescription_Short]];
self.artNumber = [self checkNullAndNill:dict[kArtNumber]];
self.afLink = [self checkNullAndNill:dict[kDeeplink1]];
self.imageURL = [self checkNullAndNill:dict[kImg_url]];
_isCart = @(NO);
}
return self;
}
//==============================================================================
- (id)checkNullAndNill:(id)object
{
if (object == nil)
return @"";
else if (object == [NSNull null])
return @"";
else
return object;
}
//============================================================================== …Run Code Online (Sandbox Code Playgroud) 我的视图控制器中有这个:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.dataObjects = data;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在我的 CellSubclass.m 中,我有:
- (id) initWithFrame:(CGRect)frame
{
// You can call initWithFrame: method here as our base constructor of the UIView super class
self = [super initWithFrame:frame];
if (self)
{
// call functions that need access to data but data is nil
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
所以在我的视图控制器的生命周期中, CellSubclass 在数据准备好之前立即被调用。有没有像 viewWillAppear 这样的函数或者我也可以传递数据的东西?除了 initWithFrame 之外,如何从我的视图控制器中调用函数?我可以通过其他方式传递数据吗?
谢谢。
objective-c uiviewcontroller ios uicollectionview uicollectionviewcell
这样做的过程是什么?
我发现sizeForItemAtIndexPath但是我不确定我在那种方法中做了什么.我是否创建了一个新的UICollectionViewCell,添加我的内容,然后返回该单元格的大小?
我是否需要打开AutoLayout并设置约束?如果已经回答,我道歉.我一直在搜索stackoverflow,但还没有找到一个有效的解决方案.
我正在从JSON响应创建一个集合视图.Json响应具有高分辨率图像和大约2000年的图像数量.由于数据来自第三方,我无法控制图像大小.
执行:
JSON示例:
{
"imagelist": [
{
"largeImage": "https://amazingphotos/01.jpg"
},
{
"largeImage": "https://amazingphotos/02.jpg"
},
}
Run Code Online (Sandbox Code Playgroud)
在UICollectionViewController中访问JSON:
Alamofire.request(.GET, dataURL)
.responseJSON { _, _, result in
if var json:JSON = JSON(result.value!){
self.imageURLs = json["imagelist"].arrayValue
self.collectionView?.reloadData()
}
}
Run Code Online (Sandbox Code Playgroud)
在cellForItemAtIndexPath中,我将URL传递给单元格而不是图像本身.
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as! CollectionViewCell
cell.imageData = imageURLs[indexPath.row]
Run Code Online (Sandbox Code Playgroud)
在自定义Cell我只是这样做:
var imageData:SwiftyJSON.JSON?{
didSet{
self.setup()
}
}
func setup(){
if let imageURLString = self.imageData?["largeImage"]{
let imageURL = NSURL(string: imageURLString.stringValue)
self.imageThumbnail.hnk_setImageFromURL(imageURL!)
}
Run Code Online (Sandbox Code Playgroud)
问题:当我加载集合视图时,它会正确显示图像,一旦我开始滚动它就会在新单元格中显示我之前的图像几秒钟,然后刷新.
我多次使用集合视图但从未使用过如此多的数据.请指教.TIA
我是新手Swift,请多多包涵。
我创建了一个UICollectionView水平滚动的,我的问题是:如何以编程方式移动到选定的图标?
我已经用过了didSelectedPath,接下来呢?
我有一个 UICollectionView,它看起来像一个 tableView,我希望单元格只能水平滑动。
我已经设法让它们四处移动,但问题是我也可以将它们向上移动,基本上我可以向任何方向移动它们,例如,当您删除它时,我希望它们像 tableViewCell 一样滑动。
最后,我希望能够水平地将一个单元格从屏幕上滑出
我附上了一张集合视图现在看起来如何的图像和一个四处移动的单元格(红色的)
我有一个带有多个 UI 元素的自定义 UICollectionViewCell,在代码中使用 AutoLayout 设置进行布局。
在较大的设备(iPhone 6 及更高版本)上,一切都按预期工作。
然而,在较小的设备上,多行 UILabel 会中断,但仅(似乎)在重用后才会中断。
在初始显示时,第一个单元格如下所示:
在单元格滚动出屏幕并重新打开后,它看起来像这样:
这些是在标签上设置的约束:
descriptionLabel.centerXAnchor.constraint(equalTo: firstButton.centerXAnchor),
descriptionLabel.leadingAnchor.constraint(equalTo: otherLabel.leadingAnchor),
descriptionLabel.topAnchor.constraint(equalTo: firstButton.bottomAnchor, constant: 15),
secondButton.topAnchor.constraint(greaterThanOrEqualTo: descriptionLabel.bottomAnchor, constant: 20),
Run Code Online (Sandbox Code Playgroud)
我觉得这与greaterThanOrEqualTo约束有关,但如果我用一个普通的旧equalTo约束替换它,布局就会变得疯狂,标签缩小到只适合一行。
我正在使用Kingfisher下载和缓存我的图像。我正在使用CustomImageView下面的自定义类来扩展ImageView。我还使用我的自定义UITableViewCell在CustomCell类文件中的didSet属性内调用loadImage方法。
下面的方法loadImage是发生所有魔术的地方。
class CustomImageView : UIImageView {
var lastUrlLoaded: String?
func loadImage(url:String) {
lastUrlLoaded = url
ImageCache.default.retrieveImage(forKey: url, options: nil) { (image, cacheType) in
if let image = image {
self.image = image
return
}
else {
guard let url = URL(string: url) else { return }
self.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil) {
(image, error, cacheTye, _) in
if let err = error {
self.kf.indicatorType = .none
DispatchQueue.main.async {
self.image = #imageLiteral(resourceName: …Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×5
objective-c ×3
autolayout ×2
kingfisher ×1
swipe ×1
uiimage ×1
uiimageview ×1
uitableview ×1
xcode ×1