RootViewController.m
#import "RootViewController.h"
#import "TestTableImageAppDelegate.h"
// table view 리스트에 표현되는 데이터 배열
NSMutableArray *arr;
UIImage *img1; // 아이템의 아이콘 용 그림
@implementation RootViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arr count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
if( [arr count] == indexPath.row )
{ // 마지막 아이템은 새로운 아이템으로 사용
cell.text = @"New Item...";
}
else
{
cell.text = [arr objectAtIndex: indexPath.row];
cell.image = img1;
}
return cell;
}
// 리스트 아이템 그릴때 마다 호출되어서 에디트 모드 시 삭제 용인지 추가 용인지 결정
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{ // 마지막 아이템은 + 모양의 추가 모양을 만듬
if(indexPath.row == [arr count]) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to add the Edit button to the navigation bar.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
arr = [[NSMutableArray alloc] initWithObjects: @"one", @"two", @"three", nil];
img1 = [UIImage imageNamed: @"img1.png"];
}
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[arr removeObjectAtIndex: indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
// 아이템을 배열 마지막에 추가한 후 테이블 다시 그리기
[arr addObject: @"It's new"];
[tableView reloadData];
}
}
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
int toRow;
if( toIndexPath.row == [arr count] )
// fix bug
toRow = [arr count]-1;
else
toRow = toIndexPath.row;
[arr exchangeObjectAtIndex: fromIndexPath.row withObjectAtIndex: toRow];
}
// Override to support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
// 마지막은 이동 불가 = 모양 안나타남
if( [arr count] == indexPath.row )
return NO;
return YES;
}
3. 실행하기
int toRow;
if( toIndexPath.row == [arr count] )
// fix bug
toRow = [arr count]-1;
else
toRow = toIndexPath.row;
[arr exchangeObjectAtIndex: fromIndexPath.row withObjectAtIndex: toRow];
'KB > iPhone 개발' 카테고리의 다른 글
iPhone: Editing on UITableView (0) | 2009.01.23 |
---|---|
iMobileCinema - 아이팟 터치에서 사파리에서 플래시 보기 (2) | 2009.01.23 |
iPhone UITableViewController 사용하기 (0) | 2009.01.21 |
iPhoneSDK How to make first Instance (0) | 2009.01.20 |
iPhone: UIImageView 사용하기 (0) | 2009.01.19 |