ios开发基础问题总结
1,如何进入应用后首先展示图片,然后程序继续运行
只需要将所需要的图片引入工程,并重命名为Default.png(注意大小写)。另外,在模拟器中调试时Default.png可以放在任意文件夹下,但是据说真机上运行时Default.png必须位于根目录下(这个没有验证过)。
2,如何发送HTTP请求
iPhone OS内置有CFNetWorik框架可以使用。但是这里推荐使用ASIHttpRequest框架,官网上面有详细的说明和使用教程。
3,如何使用JSON数据
这个没得说,当然是json-framework,苹果官方都是使用它来解析JSON的。
4,如何点击屏幕空白位置时,隐藏键盘
建立一个全屏幕的button,将类型设置为Custom,这样它就没有边框了,然后将该按钮拖到相应图层位置让它不至于遮住其他元素,最后将它关联到相应的target-action就OK了。如下图所示:
5,如何在IB中使用UITableView中的自定义的Cell
自定义UITableViewCell的子类,这里详细介绍了实现方式。
6,如何根据字数控制UILabel的高度,以及如何定位Cell中的元素
123456789101112131415161718192021-
?(
UITableViewCell?
*
)
tableView
:
(
UITableView?
*
)
tableView cellForRowAtIndexPath
:
(
NSIndexPath
*
)
indexPath?
{
????
/
*
?其他代码?
*
/
?????
/
/
?UILable的宽度
????
CGFloat contentWidth?
=
?259.0
00
;
????
/
/
?根据字体计算高度
????
UIFont?
*
baseFone?????
=
?[UIFont systemFontOfSize
:
13
];
????
/
/
?获取UILabel将要显示的数据
????
NSString?
*
content
????=
?[
data
?objectForKey
:
@
"content"
];
????
/
/
?1000
仅仅是个约数,只要保证
content
能显示完整就行
????
/
/
?在IB中要把UILabel的换行类型设置为UILineBreakModeWordWrap
????
CGSize? contentSize??
=
?[
content
?sizeWithFont
:
baseFone constrainedToSize
:
CGSizeMake
(
contentWidth
,
10000
)
?lineBreakMode
:
UILineBreakModeWordWrap];
????
/
/
?IB中一定要将UILabel的Layout属性的Lines设置为
0
,以保证宽度自适应
????
CGRect? frame????????
=
?[cell.contentLabel textRectForBounds
:
cell.contentLabel.frame limitedToNumberOfLines
:
0
];
????
frame.size.height????
=
?size.height;
????
/
/
?通过frame.origin.x 和 frame.origin.y来定位元素
????
frame.origin.y???????
=
?otherHeight?
+
?cell.otherLabel.frame.size.height;
????
cell.contentLabel.frame?
=
?frame;
?????
/
*
?其他代码?
*
/
}
1234567891011121314151617-
?(
CGFloat
)
tableView
:
(
UITableView?
*
)
tableView heightForRowAtIndexPath
:
(
NSIndexPath?
*
)
indexPath?
{
????
/
*
?其他代码?
*
/
?????
/
/
?UILable的宽度
????
CGFloat contentWidth?
=
?259.0
00
;
????
/
/
?根据字体计算高度
????
UIFont?
*
baseFone?????
=
?[UIFont systemFontOfSize
:
13
];
????
/
/
?获取UILabel将要显示的数据
????
NSString?
*
content
????=
?[
data
?objectForKey
:
@
"content"
];
????
/
/
?1000
仅仅是个约数,只要保证
content
能显示完整就行
????
/
/
?在IB中要把UILabel的换行类型设置为UILineBreakModeWordWrap
????
CGSize? contentSize??
=
?[
content
?sizeWithFont
:
baseFone constrainedToSize
:
CGSizeMake
(
contentWidth
,
10000
)
?lineBreakMode
:
UILineBreakModeWordWrap];
?????
/
*
?下面就可以通过contentSize.height计算Cell的高度了?
*
/
?????
/
*
?其他代码?
*
/
}
7,如何异步加载图片等数据
大致思路为两步:(1)?加载tableView时读取图片缓存,如果存在则显示,如果不存在则异步下载;(2) 下载图片信息成功,更新图片缓存,重新加载tableView的数据
示例代码:
12345678910111213141516171819202122232425262728293031323334353637383940414243-
?(
UITableViewCell?
*
)
tableView
:
(
UITableView?
*
)
tableView cellForRowAtIndexPath
:
(
NSIndexPath
*
)
indexPath?
{
????
/
*
?其他代码?
*
/
?????
/
/
?用户头像
????
UIImage?
*
userPic?
=
?[self cachedImageForUrl
:
[NSURL URLWithString
:
url
]];
????
cell.userPicView.
image
?=
?userPic;
?????
/
*
?其他代码?
*
/
}
?-
?(
UIImage?
*
)
cachedImageForUrl
:
(
NSURL?
*
)
url
?{
????
id
?cachedObject?
=
?[self.cachedImage objectForKey
:
url
];
????
if
?(
cachedObject?
=
=
?nil
)
?{
????????
/
/
防止reloadData时重复提交请求,添加占位符
????????
[self.cachedImage setObject
:
@
"Loading..."
?forKey
:
url
];
????????
/
/
异步下载图片
????????
ASIHTTPRequest?
*
picRequest?
=
?[ASIHTTPRequest requestWithURL
:
url
];
????????
picRequest.delegate?
=
?self;
????????
picRequest.didFinishSelector?
=
?@selector
(
didFinishRequestImage
:
)
;
????????
picRequest.didFailSelector???
=
?@selector
(
didFailRequestImage
:
)
;
????????
[self.queue addOperation
:
picRequest];
????????
/
/
?更新状态栏的网络标示
????????
[UIApplication sharedApplication].networkActivityIndicatorVisible?
=
?YES;
????
}
else if
?(
?![cachedObject isKindOfClass
:
[UIImage?
class
]]?
)
?{
????????
cachedObject?
=
?nil;
????
}
????
return
?cachedObject;
}
?-
?(
void
)
didFinishRequestImage
:
(
ASIHTTPRequest?
*
)
request?
{
????
NSData?
*
imageData?
=
?[request responseData];
????
UIImage?
*
image
?=
?[UIImage imageWithData
:
imageData];
????
if
?(
image
?!
=
?nil
)
?{
????????
[self.cachedImage setObject
:
image
?forKey
:
request.
url
];
????????
[self.tableView reloadData];
????
}
????
[UIApplication sharedApplication].networkActivityIndicatorVisible?
=
?NO;
}
?-
?(
void
)
didFailRequestImage
:
(
ASIHTTPRequest?
*
)
request?
{
????
NSLog
(
@
"Error Download Image: %@"
,
?request.
error
)
;
????
[UIApplication sharedApplication].networkActivityIndicatorVisible?
=
?NO;
}
8,ASIHttpRequest的一个bug的修复
请见我的一个提问和解决(6楼)