Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView) ใน Table View (UITableView) เราสามารถเรียก Mode สำหรับที่จะทำการ Delete Cell หรือ Rows นั้น ๆ ได้ด้วยการใช้ความสามารถของ Table View ซึ่งหลาย ๆ App ค่อนข้างจะมีรูปแบบเดียวกัน ทั้งนี้ Apple ได้ Guideline และแนะนำให้ Developer ทั้งหลาย พยายามเรียกใช้เครื่องมือและออกแบบ User Interface ที่เป็น Standard ที่อยู่บน Object ต่าง ๆ เพื่อให้โปรแกรมนั้นเป็นไปในทิศทางเดียวกัน

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)


สำหรับ Table View (UITableView) เราสามารถเรียก Mode สำหรับ Edit/Delete ได้ง่าย ๆ เพียงเรียกใช้ property ว่า setEditing:YES หรือจะกำหนด delegate method ของ TableView ที่มีชื่อว่า UITableViewCellEditingStyle ก็จะสามารถที่จะทำการ Slide หรือ Swipe ในแต่ล่ะ Cell เพื่อที่จะ Delete Rows หรือ Cell นั้น ๆ ได้ทันที ซึ่งถ้าเราได้ใช้ iPhone หรือ iPad ประจำอยู่แล้วก็จะคุ้น ๆ กับ Interface นี้ดี

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete)

ตัวอย่าง Table View (UITableView) และ Mode สำหรับ Delete บน App ของ iPhone


การเรียก Mode Edit
[myTable setEditing:YES animated:YES];


การเรียก delegate method ของ TableView สำหรับ Slide หรือ Swipe
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        [myObject removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}









Example การสร้าง Mode สำหรับ setEditing / Delete บน Table View (UITableView)

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

เริ่มต้นด้วยการสร้าง Application บน Xcode แบบ Single View Application

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

เลือกและไม่เลือกดังรูป

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

ตอนนี้หน้าจอของเราจะยังว่าง ๆ

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

ให้สร้าง Navigation Bar บน View

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

เปลี่ยน Title ให้ซะหน่อย

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

ตามด้วย Bar Button Item สำหรับเป็นปุ่ม Edit / Cancel

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

ลาก Table View มาไว้ที่หน้าจอ View

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

คลิกขวาที่ Table View ให้ทำการเชื่อม dataSource และ delegate กับ File's Owner

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

ใน Class ของ .h ให้เชื่อม IBOutlet และ IBAction ดังรูป จากนั้นให้เขียน Code ดังนี้

ViewController.h
//
//  ViewController.h
//  tableViewDelete
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
    
    IBOutlet UITableView *myTable;
    
    IBOutlet UIBarButtonItem *itemEdit;
    
}

- (IBAction)btnEdit:(id)sender;

@end


ViewController.m
//
//  ViewController.m
//  tableViewDelete
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *myObject;
    BOOL boolDelete;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    boolDelete = FALSE;
    
    myObject = [[NSMutableArray alloc] init];
    //myObject = [NSMutableArray arrayWithObjects:@"ThaiCreate.Com 1",@"ThaiCreate.Com 2", nil];
    [myObject addObject:@"ThaiCreate.Com 1"];
    [myObject addObject:@"ThaiCreate.Com 2"];
    [myObject addObject:@"ThaiCreate.Com 3"];
    [myObject addObject:@"ThaiCreate.Com 4"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return myObject.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // Use the default cell style.
        cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
                                       reuseIdentifier : CellIdentifier] autorelease];
    }
    
    
    NSString *cellValue = [myObject objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
    
    return cell;
}

// Show Delete Item
- (IBAction)btnEdit:(id)sender {
    if(!boolDelete)
    {
        boolDelete = TRUE;
        [myTable setEditing:YES animated:YES];
        [itemEdit setTitle:@"Cancel"];
    }
    else
    {
        boolDelete = FALSE;
        [myTable setEditing:NO animated:YES];
        [itemEdit setTitle:@"Edit"];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)
  editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        [myObject removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [myTable release];
    [itemEdit release];
    [super dealloc];
}

@end









เกี่ยวกับ Code

// Code นี้สำหรับ สลับเปิด-ปิด Mode - (IBAction)btnEdit:(id)sender { if(!boolDelete) { boolDelete = TRUE; [myTable setEditing:YES animated:YES]; [itemEdit setTitle:@"Cancel"]; } else { boolDelete = FALSE; [myTable setEditing:NO animated:YES]; [itemEdit setTitle:@"Edit"]; } }


// Code นี้สำหรับ Slide หรือ Swipe แล้วขึ้น Delete - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES if you want the specified item to be editable. return YES; }


// Code นี้สำหรับ เมื่อคลิก Delete เราจะได้สำแหน่งของ Index ที่ทำการลบบน Table View - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //add code here for when you hit delete [myObject removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } }


Screenshot

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

แสดง Table View ซึ่ง Data มาจาก Array

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

คลิกที่ Edit หรือ Swipe บน Cell เพื่อเข้าสู่ Mode

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

แสดง Mode สำหรับ Delete

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

คลิก Delete

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

ข้อมูลได้ถูกลบไปเรียบร้อยแล้ว

iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)

คลิกที่ Cancel เพื่อเข้าสู่ Mode ปกติ



เพิ่มเติม
จากตัวอย่างจะเห็นว่าเป็นเพียงแค่การลบข้อมูลที่อยู่บน TableView และ Array เท่านั้น แต่ในกรณีที่ใช้ร่วมกับข้อมูลบน Server อาจจะต้องทำการ Connect ไปยัง Web Server หลังจากที่ได้ทำการลบข้อมูลแล้ว โดยสามารถแทรกได้ที่ method นี้

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)
  editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //add code here for when you hit delete

    }
}

จากตัวอย่างจะเป็นการอ้างถึง Index ของข้อมูล สามารถดูตังอย่างได้ที่นี่

iOS/iPhone Delete Remove Data on Web Server (URL,Website)


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-12 08:49:45 / 2017-03-26 09:42:11
  Download : Download  iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)
 Sponsored Links / Related

 
iOS/iPhone Collection View and Master Detail (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Page Control (UIPageControl) and ScrollView (UIScrollView) (iPhone, iPad)
Rating :

 
iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number)
Rating :

 
iOS/iPhone AD BannerView (iAd Framework)
Rating :

 
iOS/iPhone Search Data from Web Server (URL,Website)
Rating :

 
iOS/iPhone Edit Update Data on Web Server (URL,Website)
Rating :

 
iOS/iPhone Delete Remove Data on Web Server (URL,Website)
Rating :

 
iOS/iPhone Register Form and Send Data to Web Server (PHP & MySQL)
Rating :

 
iOS/iPhone Login Username and Password from Web Server (PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่