顯示具有 UITableView 標籤的文章。 顯示所有文章
顯示具有 UITableView 標籤的文章。 顯示所有文章

2014年8月29日 星期五

取消UITableView選擇後會highlight的動畫

我總共找到幾種方法,這之間有一些差異

第一種 放在tableView:cellForRowAtIndexPath:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
但仍然會觸發tableView:didSelectRowAtIndexPath:,只是不會有highlight的動畫。

可以用這個方法避免觸發到不希望有highlight效果的row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // A case was selected, so push into the CaseDetailViewController
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (![cell selectionStyle] == UITableViewCellSelectionStyleNone) {
        // Handle tap code here
    }
}


第二種 放在viewDidLoad裡
[self.tableView setAllowsSelection:NO];
一次取消整個tableView的highlight功能,不會觸動tableView:didSelectRowAtIndexPath:。
另外這是我找到的評論:Instead of userInteraction, use tableView.allowsSelection. That stops the cell's interaction methods but doesn't block the regular UIView responder stuff.


第三種 放在tableView:cellForRowAtIndexPath
cell.userInteractionEnabled = NO;
指定特定的row停止所有與使用者互動的功能,包括向右滑動刪除的功能。


第四種
放在viewDidLoad裡
[self.tableView setUserInteractionEnabled:NO];
一次設定所有的tableView的userInteraction功能都取消。但這個方法會使得tableView連scroll的動作都沒辦法做,要注意!


第五種
直接在Storyboard上設定,點選tableView的Selection選項,選擇No Selection

用這個方法的話,不會觸動tableView:didSelectRowAtIndexPath:。

資料來源:http://stackoverflow.com/questions/190908/how-can-i-disable-the-uitableview-selection-highlighting

2014年7月10日 星期四

tableView

重新載入資料

全部
[self.tableView reloadData];
更新特定row
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
別人的文章:UITableViewController 之 dataSource 更新顯示


不知道什麼時候要加@@
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];


設定接下來要顯示的viewController的title,有兩種方式:

prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *page2 = segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    (indexPath.row == 0) ? [page2 setTitle:@"Go To Bed Time"] : [page2 setTitle:@"Wake Up Time"];
}

didSelectRowAtIndexPath,需要另外設定目標viewController的Storyboard ID。但如果需要隔空轉換畫面,也就是不用segue轉換畫面的話,比如:同一個button一不同的情況要轉換到不同的view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *page2 = [self.storyboard instantiateViewControllerWithIdentifier:@"page3"];
    (indexPath.row == 0) ? [page2 setTitle:@"Go To Bed Time"] : [page2 setTitle:@"Wake Up Time"];
    [self.navigationController pushViewController:page2 animated:YES];
}

如果轉換模式要用modal
[self presentViewController:page2 animated:YES completion:nil];
completion那裡可以放轉換過去後要執行的程式

indexPath

The NSIndexPath class represents the path to a specific node in a tree of nested array collections.
A table has sections and rows, to know where a row is you need to know the index of which section it is in and the index position it is at within that section.
UIKit adds a category to make it easier to work with UITableView's.
@interface NSIndexPath (UITableView)

+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;

@property(nonatomic,readonly) NSInteger section;
@property(nonatomic,readonly) NSInteger row;

@end
資料來源:http://stackoverflow.com/a/10807882/3295047

2014年2月11日 星期二

Add UISwitch with code.

1. 在「tableView:cellForRowAtIndexPath:」裡加入
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 30.0)];
switchControl.on = NO;
[switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchControl;

2. 再新增這個method
- (void) switchChanged:(id)sender {
    UISwitch *switchControl = sender;
    NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}

就完成了!!!


更詳細的請看蘋果官方文件