Created
May 17, 2012 07:04
-
-
Save xxd/2717120 to your computer and use it in GitHub Desktop.
iOS SDK控件:UITableView,UITextView,NSTimer,UISwitch,NSBundle,UIButton,UINavigation,UIImage,Segue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************* | |
// NSBundle ofType | |
//******************* | |
NSString *textUrl = [[NSBundle mainBundle] pathForResource:@"myText" ofType:@"txt"]; | |
textView.text = [NSString stringWithContentsOfFile:textUrl encoding:NSUTF8StringEncoding error:nil]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//NSTimer的用法 | |
//iPhone为我们提供了一个很强大得时间定时器 NSTimer,它可以完成任何定时功能: | |
//我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理delegate和事件处理方法@selector();就可以用 | |
//1 +(NSTimer *)scheduledTimerWithTimeIn | |
//2 terval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; | |
//来初始化一个 时间定时器 下面我写了一个很简单得例子: | |
-(void)initTimer | |
{ | |
//时间间隔4 NSTimeInterval timeInterval =1.0; | |
//定时器6 NSTimer | |
showTimer =[NSTimer scheduledTimerWithTimeInterval:maxShowTime | |
target:self | |
selector:@selector(handleMaxShowTimer:) | |
userInfo:nil | |
repeats:NO]; | |
} | |
//触发事件13 -(void)handleMaxShowTimer:(NSTimer *)theTimer | |
{ | |
NSDateFormatter dateFormator =[[NSDateFormatter alloc] init]; | |
dateFormator.dateFormat =@"yyyy-MM-dd HH:mm:ss"; | |
NSString *date =[dateformater stringFromDate:[NSDate date]]; | |
if([date isEqualToString:@"2010-11-09 23:59:59"]) | |
{ | |
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:TITLE_NAME | |
message:@"现在马上就有新的一天了!" | |
delegate:self | |
cancelButtonTitle:nil | |
otherButtonTitles:CONFIRM_TITLE, nil]; | |
[alert show]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//获取objectAtIndex:self.tableView.indexPathForSelectedRow.row | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
DetailViewController *detailController =segue.destinationViewController; | |
ScaryBugDoc *bug = [self.bugs objectAtIndex:self.tableView.indexPathForSelectedRow.row]; | |
detailController.detailItem = bug; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//sing NSData to store Pic from a Url | |
NSURL *url = [NSURL URLWithString: [dicPost objectForKey:@"avatar_url"]]; | |
NSData *data = [NSData dataWithContentsOfURL:url]; | |
UIImage *image = [UIImage imageWithData:data]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//*************** | |
// 添加UISwitch | |
//*************** | |
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(4.0f, 16.0f, 100.0f, 28.0f)]; | |
switchView.on = NO;//初始为Off | |
[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************** | |
//如何让UITableViewCell滚动到视图顶端 | |
//******************************** | |
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; | |
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; | |
这两个方法都可以用,第一个函数是吧当前行滚动到指定位置,(UITableViewScrollPosition)scrollPosition-这个有四个选择:top,mid,bot,,,,,第二个函数是滚动到最近的位置 | |
//******************************** | |
//多线程获取UITableView Image的例子 | |
//******************************** | |
// 经常我们会用tableView显示很多条目, 有时候需要显示图片, 但是一次从服务器上取来所有图片对用户来浪费流量, 对服务器也是负担.最好是按需加载,即当该用户要浏览该条目时再去加载它的图片.重写如下方法 | |
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UIImage *image = [self getImageForCellAtIndexPath:indexPath]; //从网上取得图片 | |
[cell.imageView setImage:image]; | |
} | |
// 这虽然解决了延时加载的问题, 但当网速很慢, 或者图片很大时(假设,虽然一般cell中的图很小),你会发现程序可能会失去对用户的响应.原因是UIImage *image = [self getImageForCellAtIndexPath:indexPath]; 这个方法可能要花费大量的时间,主线程要处理这个method.所以失去了对用户的响应.所以要将该方法提出来: | |
- (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
UIImage *image = [self getImageForCellAtIndexPath:indexPath]; | |
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; | |
[cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; | |
[pool release]; | |
} | |
//然后再新开一个线程去做这件事情 | |
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath]; | |
} | |
// 同理当我们需要长时间的计算时,也要新开一个线程去做这个计算以避免程序处于假死状态。以上代码只是示例, 还可以改进的更多, 比如从网上down下来一次后就将图片缓存起来,再次显示的时候就不用去下载. | |
//*************************** | |
//打开tableview到最后一列 | |
//*************************** | |
[self.chatTableView reloadData]; | |
int lastRowNumber = [self.chatTableView numberOfRowsInSection:0] - 1; | |
NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0]; | |
[self.chatTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO]; | |
//*************************** | |
//UITableView 基本使用方法总结 | |
//*************************** | |
//1.Controller需要实现两个delegate,分别是 UITableViewDelegate 和 UITableViewDataSource | |
//2.然后 UITableView对象的 delegate 要设置为 self。 | |
//3. 然后就可以实现这些delegate的一些方法拉。 | |
////(1)这个方法返回 tableview 有多少个section | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 1; | |
} | |
////(2)这个方法返回对应的section有多少个元素,也就是多少行。 | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return 10; | |
} | |
(3) | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; | |
////这个方法返回指定的 row 的高度。 | |
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; | |
////这个方法返回指定的 section的header view 的高度。 | |
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; | |
////这个方法返回指定的 section的footer view 的高度。 | |
//(4)返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本 | |
//的cell 类型。其中有一个主标题 cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个 image在最前头 叫 | |
//cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头, | |
//还是勾勾标记。 | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell"; | |
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier]; | |
if (cell == nil) | |
{ | |
// Create a cell to display an ingredient. | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle | |
reuseIdentifier:showUserInfoCellIdentifier] | |
autorelease]; | |
} | |
// Configure the cell. | |
cell.textLabel.text=@"签名"; | |
cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding]; | |
//设置cell线的颜色,背景颜色 | |
tableView.separatorColor = [UIColor lightGrayColor]; | |
cell.contentView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; | |
tableView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; | |
cell.textLabel.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; | |
} | |
//4.1 如果想让cell中的image固定大小,需要在你的cell view controller中加入以下方法 | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
self.imageView.bounds = CGRectMake(0,0,75,75); | |
self.imageView.frame = CGRectMake(0,0,75,75); | |
self.imageView.contentMode = UIViewContentModeScaleAspectFit; | |
CGRect tmpFrame = self.textLabel.frame; | |
tmpFrame.origin.x = 77; | |
self.textLabel.frame = tmpFrame; | |
tmpFrame = self.detailTextLabel.frame; | |
tmpFrame.origin.x = 77; | |
self.detailTextLabel.frame = tmpFrame; | |
} | |
////(5)返回指定的 section 的header的高度 | |
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section | |
{ | |
if (section ==0) | |
return 80.0f; | |
else | |
return 30.0f; | |
} | |
////(6)返回指定的section 的 header 的 title,如果这个section header 有返回view,那么title就不起作用了。 | |
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section | |
{ | |
if (tableView == tableView_) | |
{ | |
if (section == 0) | |
{ | |
return @"title 1"; | |
} | |
else if (section == 1) | |
{ | |
return @"title 2"; | |
} | |
else | |
{ | |
return nil; | |
} | |
} | |
else | |
{ | |
return nil; | |
} | |
} | |
////(7)返回指定的 section header 的view,如果没有,这个函数可以不返回view | |
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section | |
{ | |
if (section == 0) | |
{ | |
UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView" | |
owner: self | |
options: nil] lastObject]; | |
else | |
{ | |
return nil; | |
} | |
} | |
////(8)当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。 | |
TableView.allowsSelection=YES; | |
cell.selectionStyle=UITableViewCellSelectionStyleBlue; | |
//如果不希望响应select,那么就可以用下面的代码设置属性: | |
.allowsSelection=NO; | |
//下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。 | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (indexPath.section == 1) | |
{ | |
return; | |
} | |
else if(indexPath.section==0) | |
{ | |
switch (indexPath.row) | |
{ | |
//聊天 | |
case 0: | |
{ | |
[self onTalkToFriendBtn]; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
else | |
{ | |
return ; | |
} | |
//常规的跳转到detail页: | |
PostDetail *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"PostDetail_tvc"]; | |
//跳转到detail页也可以传值: | |
NSDictionary *dicPost = [listData objectAtIndex:indexPath.row]; | |
detail.postDetail = dicPost; | |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
[self.navigationController pushViewController:detail animated:YES]; //这个方法还可以popViewController | setViewController | |
} | |
//如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置 | |
cell.selectionStyle = UITableViewCellSelectionStyleNone; | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
//cell被选中后的颜色不变 | |
cell.selectionStyle = UITableViewCellSelectionStyleNone; | |
//在这个案例中,我们将backgroundView设置为显示一幅外边缘有一定透明度的图片,因此backgroundColor的显示将位于图片背后。我们将selectedBackgroundView设置为一个几乎透明的黑色长方形,当单元格被选中时图片将变黑。同时我们为textLabel提供整幅背景色,于是我们其他的内容透过背景色显示: | |
UIImageView* v = [[UIImageViewalloc] initWithFrame:cell.bounds]; | |
v.contentMode = UIViewContentModeScaleToFill; | |
v.image = [UIImageimageNamed:@"linen.png"]; | |
cell.backgroundView = v; | |
} | |
////(9)如何设置tableview 每行之间的 分割线 | |
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; | |
//如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone 即可。 | |
////(10)如何设置 tableview cell的背景颜色 | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
//设置背景颜色 | |
cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1]; | |
} | |
////(11)这个函数响应,用户点击cell 右边的 箭头(如果有的话) | |
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath | |
////(12)如何设置tableview 可以被编辑 | |
////首先要进入编辑模式: | |
[TableView setEditing:YES animated:YES]; | |
////如果要退出编辑模式,肯定就是设置为NO | |
////返回当前cell 要执行的是哪种编辑,下面的代码是 返回 删除 模式 | |
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
return UITableViewCellEditingStyleDelete; | |
} | |
-(void) tableView:(UITableView *)aTableView | |
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle | |
forRowAtIndexPath:(NSIndexPath *)indexPath | |
////通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。 | |
-(void) tableView:(UITableView *)aTableView | |
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle | |
forRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
[chatArray removeObjectAtIndex:indexPath.row]; | |
[chatTableView reloadData]; | |
} | |
////(13)如何获得 某一行的CELL对象 | |
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; | |
--EOF-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) header:(NSString *) sharp { | |
NSRange cursorPosition = [markdownTextView selectedRange]; | |
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[markdownTextView text]]; | |
[tfContent insertString:sharp atIndex:cursorPosition.location]; | |
[markdownTextView setText:tfContent]; | |
} | |
- (void) replaceCharactersInRange:(NSRange)range withString:(NSString *)newText | |
{ | |
markdownTextView.scrollEnabled = NO; | |
NSMutableString *textStorage = [markdownTextView.text mutableCopy]; | |
[textStorage replaceCharactersInRange:range withString:newText]; | |
//replace text but undo manager is not working well | |
[[self.undoManager prepareWithInvocationTarget:self] replaceCharactersInRange:NSMakeRange(range.location, newText.length) | |
withString:[textStorage substringWithRange:range]]; | |
NSLog(@"before replacing: canUndo:%d", [self.undoManager canUndo]); //prints YES | |
markdownTextView.text = textStorage; | |
NSLog(@"after replacing: canUndo:%d", [self.undoManager canUndo]); //prints NO | |
if (![self.undoManager isUndoing])[self.undoManager setActionName:@"replace characters"]; | |
//new range: | |
range.location = range.location + newText.length; | |
range.length = 0; | |
markdownTextView.selectedRange = range; | |
markdownTextView.scrollEnabled = YES; | |
} | |
//replace the selection with a word in UITextView | |
NSRange range = textView.selectedRange; | |
if (range.location != NSNotFound) { | |
NSString *replacement = @"some word"; | |
textView.text = [textView.text stringByReplacingCharactersInRange:range | |
withString:replacement]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//*************** | |
// 增加title | |
//*************** | |
self.title = [NSStringstringWithFormat:@"与%@的私信",currentName]; | |
//*************** | |
//check if a view exists | |
//*************** | |
UIView* barview = [cell viewWithTag:123221]; | |
if (barview != nil) { | |
... | |
} | |
//Otherwise, you need to iterate through the .subviews array and check if the property matches, e.g. | |
UIView* barview = nil; | |
for (UIView* subview in cell.subviews) { | |
if ([subview isKindOfClass:[BarView class]]) { | |
barview = subview; | |
break; | |
} | |
} | |
if (barview != nil) { | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
自定义按钮 http://www.cocoachina.com/bbs/read.php?tid=73570&uid=39045&page=1