我试图在UICollectionView中预先选择第一个对象/ UICollectionViewCell?我试过了:
self.dateCollectionView.allowsMultipleSelection=NO;
[self.dateCollectionView selectItemAtIndexPath:0 animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
[self collectionView:self.dateCollectionView didSelectItemAtIndexPath:0];
[self.dateCollectionView reloadData];
viewDidLoad
中的。
以下是我的UICollectionView
方法;
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.titles.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor= [UIColor clearColor];
UILabel * dateLabel = (UILabel *)[cell viewWithTag:1];
UILabel * subText = (UILabel *)[cell viewWithTag:2];
subText.text=self.titles[indexPath.row];
subText.adjustsFontSizeToFitWidth=YES;
if (cell.selected) {
cell.backgroundColor = [UIColor blueColor]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor redColor]; // Default color
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor redColor]; // Default color
}
- (BOOL)collectionView:(UICollectionView *)collectionView
shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView
shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
return YES;
}
答案 0 :(得分:18)
在viewDidAppear
:
NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection:0];
[self.dateCollectionView selectItemAtIndexPath:indexPathForFirstRow animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.dateCollectionView didSelectItemAtIndexPath:indexPathForFirstRow];
答案 1 :(得分:9)
对于 Swift 3.0.1 您可以尝试:
self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
或
self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition(rawValue: 0))
对于Objective-C你可以试试这个:
self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
注意:您应该在 viewDidAppear
中使用它答案 2 :(得分:7)
对于swift 3
在此重载collectionView.selectItem
collectionView(UICollectionView, IndexPath)
这是我的代码,在此代码中,我选择了indexPath.row = 0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ScenarioCollectionView.dequeueReusableCell(withReuseIdentifier: "ReuseScenarioCollectionViewCell", for: indexPath as IndexPath) as! ScenarioCollectionViewCell
if (indexPath.row == 0){
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
cell.layer.borderColor=UIColor.gray.cgColor
}else{
cell.layer.borderColor=UIColor.white.cgColor
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderColor = UIColor.gray.cgColor
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath as IndexPath)
cell?.layer.borderColor = UIColor.white.cgColor
collectionView.deselectItem(at: indexPath, animated: true)
}
答案 3 :(得分:5)
我通过继承UICollectionView
并在layoutSubviews
中选择所需项目来解决这个问题:
class InitialSelectionCollectionView: UICollectionView {
var initialSetupPerformed: Bool = false
var initialSelectedIndexPath: IndexPath!
override func layoutSubviews() {
super.layoutSubviews()
if !initialSetupPerformed && initialSelectedIndex != nil{
selectItem(at: initialSelectedIndexPath, animated: false, scrollPosition: .centeredHorizontally)
initialSetupPerformed = true
}
}
}
然后,当您初始化自定义集合视图时,只需将IndexPath
设置为initialSelectedIndexPath
答案 4 :(得分:4)
对我来说,将它放入viewDidAppear:
会导致选择一秒,因此用户将看到两种状态(即未选择和选择)。
为了避免这种情况,我把它放在viewWillAppear:
而不是像魅力一样工作
override func viewWillAppear(_ animated: Bool) {
let selectedIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left)
}
答案 5 :(得分:2)
对于Swift 3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//auto selected 1st item
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
self.collectionView?.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: .top)
}
答案 6 :(得分:1)
最初用于选择单元格的快捷代码
func selectinitialCell() {
let cell = venueTypeCollectionView.cellForItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! SearchTypeCollectionViewCell
self.indexPathOfSelectedItem = NSIndexPath(forItem: 0, inSection: 0)
cell.venueType.textColor = UIColor.whiteColor()
cell.selected = true
}
但是这不会调用委托函数,例如didSelectItemAtIndexPath,didDeselectItemAtIndexPath。
答案 7 :(得分:1)
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded]; //important
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
答案 8 :(得分:0)
就我而言。我通过用- - - - - - - - - - BEGIN REQUEST - - - - - - - - - -
PUT https://nyc3.digitaloceanspaces.com/cmtz/UploadedFiles/Mixtapes/test-upload-mixtape-20/cover.jpg HTTP/1.1
Host: nyc3.digitaloceanspaces.com:443
image/jpeg: System.Byte[]
x-amz-acl: public-read
Content-Type: image/jpeg
Content-MD5: ****************
x-amz-content-sha256: UNSIGNED-PAYLOAD
x-amz-date: 20191104T030850Z
Authorization: AWS4-HMAC-SHA256 Credential=*****************/20191104/us-east-1/s3/aws4_request, SignedHeaders=content-md5;host;x-amz-acl;x-amz-content-sha256;x-amz-date, Signature=fa6945a220238a7f877fb62d898d5596d9199860455499cec6bedccfb9cae0ed
- - - - - - - - - - END REQUEST - - - - - - - - - -
- - - - - - - - - - BEGIN RESPONSE - - - - - - - - - -
HTTP/1.1 403 Forbidden
x-amz-request-id: tx00000000000000ae68c6c-005dbf9642-334525e-nyc3a
Accept-Ranges: bytes
Date: Mon, 04 Nov 2019 03:08:50 GMT
Strict-Transport-Security: max-age=15552000; includeSubDomains; preload
Content-Length: 192
Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8"?><Error><Code>SignatureDoesNotMatch</Code><RequestId>tx00000000000000ae68c6c-005dbf9642-334525e-nyc3a</RequestId><HostId>334525e-nyc3a-nyc</HostId></Error>
- - - - - - - - - - END RESPONSE - - - - - - - - - -
的{{1}}类子类解决了这个问题
public async void uploadS3File(String filePath, String key, string type)
{
Console.WriteLine($"Uploading Object: {filePath}");
try
{
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("x-amz-acl", "public-read");
var minio = new MinioClient(Configuration.S3_HOST_ENDPOINT,
Configuration.S3_ACCESS_KEY, Configuration.S3_SECRET_KEY).WithSSL();
minio.SetTraceOn();
await minio.PutObjectAsync(Configuration.Bucket,
key, filePath, contentType: type, metaData: map);
}
catch (Exception ex)
{
Console.WriteLine($"Minio Exception: {ex}");
}
}
然后在ViewController中使用它
UICollectionView