我在我的项目中使用了UICollectionView。我开始以编程方式完成所有这些工作(在swift中)。 这是我的代码,它启动并将collectionView添加到屏幕:
GUI
全大写字母变量是我之前创建的常量。
一切顺利,除非我从纵向切换到横向,每行中的项目数量不会改变,但切换到横向时,行中的项目应该比以前更多。
我试图覆盖此功能:
public class Anagram {
Map<String, HashSet<String>> anagrams = Collections.synchronizedMap(new HashMap<String, HashSet<String>>());
List<String> glossary;
AtomicInteger availableProcessors = new AtomicInteger();
AtomicInteger start = new AtomicInteger();
AtomicInteger end = new AtomicInterger(); // the end of current part
Anagram(Glossary glossary) {
this.glossary = new ArrayList<>(glossary.getGlossary());
}
public void findAnagrams() {
availableProcessors.set(Runtime.getRuntime().availableProcessors());
end.set(0);
start.set(0);
buildAnagramMap();
// displayAnagrams(); // not used in this part of code
}
/**
* Builds map of anagrams
*/
private void buildAnagramMap() {
System.out.println("Starting build anagrams map at thread " + Thread.currentThread().getName());
if (glossary != null) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < availableProcessors.get(); i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.err.println("Thread " + Thread.currentThread().getName() + " started");
end.set(start.get() + glossary.size() / availableProcessors.get());
// if not divisible by number of available processors then we add residue to last thread
if ((glossary.size() % availableProcessors.get() > 0) &&
((glossary.size() - end.get()) < (glossary.size() / availableProcessors.get()))) {
end.set(glossary.size());
}
for (int i = start.get(); i < end.get(); i++) {
String key = sortString(glossary.get(i));
HashSet<String> val = anagrams.get(key);
if (val == null) {
val = new HashSet<>();
anagrams.put(key, val);
}
val.add(glossary.get(i));
start.set(start.get() + 1);
}
System.err.println("Completed thread " + Thread.currentThread().getName());
}
});
}
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
System.err.println("Glossary doesn't exist");
}
System.out.println(Thread.currentThread().getName() + " successfully ended");
}
虽然有效但是在旋转iPhone时需要大约半秒钟来填充行(它以一种丑陋的方式缓慢地完成工作)。
我该如何解决?