我有UICollectionView,其上包含很多自定义单元格。当用户长按时,我有一个长按手势,然后单元格开始摇动&删除按钮被添加到它们上。当我按下删除按钮时,单元格将从集合视图中删除。
长按代码。
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
p = [gestureRecognizer locationInView:self.collection_view];
NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
{
return;
}
if (indexPath == nil)
{
NSLog(@"couldn't find index path");
}
else
{
[[NSUserDefaults standardUserDefaults]setValue:@"yes" forKey:@"longPressed"];
[self.collection_view reloadData];
}
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
NSLog(@"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
pgr
= [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handePanPress:)];
// To detect after how many seconds you want shake the cells
pgr.delegate = self;
[self.collection_view addGestureRecognizer:pgr];
//show the done button here
navButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(navBtnDone:)];
self.navigationItem.rightBarButtonItem = navButtonDone;
}
当长手势开始时,我也会在按下导航栏按钮时在导航栏上添加右键我停止动画&删除删除按钮。我可以删除iPhone 5s中的删除按钮,但不能删除iPhone 6中的删除按钮。
下面是
的代码- (IBAction)navBtnDone:(id)sender
{
if([[[NSUserDefaults standardUserDefaults]valueForKey:@"longPressed"] isEqualToString:@"yes"])
{
[[NSUserDefaults standardUserDefaults]setValue:@"no" forKey:@"longPressed"];
[_deleteButton removeFromSuperview];
[self.collection_view reloadData];
[self.collection_view removeGestureRecognizer:pgr];
self.navigationItem.rightBarButtonItem=nil;
}
}
这里我刚刚添加了[_deleteButton removeFromSuperview];
如何在每个功能中获得单元格&删除删除按钮。
答案 0 :(得分:0)
要根据索引路径检索单元格,可以调用
UICollectionViewCell *someCell = [myCollectionView cellForItemAtIndexPath:indexPath];
然后用
删除删除按钮if (someCell) {
// Remove the delete button on the cell.
[someCell.deleteButton removeFromSuperView];
}
答案 1 :(得分:0)
有两种方式。
在你的.h
public class NotificationServices extends Service
{
@Override
public IBinder onBind(Intent arg0) {return null;}
@Override
public void onCreate() {super.onCreate();}
@SuppressWarnings("static-access")
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
JSONObject jsonObject = Utility.jsonObjectFromString(intent.getStringExtra("content"));
DeadLinesList deadlinesList = new DeadLinesList(jsonObject);
Date currentdate = Utility.removeTime(new Date());
String message = "";
for (DeadLine deadline : deadlineList.getDeadline()){
if (deadline.getDateDeadline()!= null && Utility.removeTime(deadline.getDateDeadline()).compareTo(currentdate) == 0){
message += deadline.getDeadlineName()+"\n";
}
}
if (message.length()>0){
NotificationCompat.Builder b = new NotificationCompat.Builder(Utility.getContext());
Intent newIntent = new Intent(Utility.getContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(Utility.getContext(),0, newIntent, 0);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Message")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND).build();
NotificationManager notificationManager = (NotificationManager) Utility.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {super.onDestroy();}
}
在您的手势识别器中
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.alarm.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:largeHeap="true">
<receiver android:name=".Notifications.NotificationReceiver">
<intent-filter>
<action android:name="ALARM_ACTION"/>
</intent-filter>
</receiver>
<service android:name=".Notifications.NotificationServices"
android:enabled="true">
</service>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|orientation|screenSize">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在“删除”按钮操作
中 NSIndexPath *globalIndexPath
将Indexpath.row值设置为Barbutton的标记值
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
p = [gestureRecognizer locationInView:self.collection_view];
globalIndexPath = [self.collection_view indexPathForItemAtPoint:p];
//Other Stuff
}
}
在你的按钮操作
中- (IBAction)navBtnDone:(id)sender
{
//Other Stuff
UICollectionViewCell *cell = [myCollectionView cellForItemAtIndexPath: globalIndexPath];
if (cell) {
[cell.deleteButton removeFromSuperView];
}
}