//行的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NewNotificationCell *cell = (NewNotificationCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row];
labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:15] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, 1000) lineBreakMode:UILineBreakModeWordWrap];
cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.myLabel.numberOfLines = 0;
[cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:15]];
cell.myLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
return labelSize.height+30;
}
//显示每行的内容。
- (NewNotificationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NewNotificationCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[[NewNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
cell = [[[NSBundle mainBundle]loadNibNamed:@"NewNotificationCell" owner:self options:nil]objectAtIndex:0];
}
//自动换行。
cell.myLabel.text = [self.dataArray objectAtIndex:indexPath.row];
labelSize = [cell.myLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:15] constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, 1000) lineBreakMode:UILineBreakModeWordWrap];
cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.myLabel.numberOfLines = 0;
[cell.myLabel setFont:[UIFont fontWithName:@"Arial" size:15]];
cell.myLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
//获取文字长度和高度。
CGSize fontSize =[cell.myLabel.text sizeWithFont:cell.myLabel.font
forWidth:cell.myLabel.frame.size.width
lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"文字长度=%f",fontSize.width);
//获取UILabel上最后一个字符串的位置。
CGPoint lastPoint;
CGSize sz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
CGSize linesSz = [cell.myLabel.text sizeWithFont:cell.myLabel.font constrainedToSize:CGSizeMake(cell.myLabel.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
if(sz.width <= linesSz.width) //判断是否折行
{
lastPoint = CGPointMake(cell.myLabel.frame.origin.x + sz.width, cell.myLabel.frame.origin.y);
}
else
{
lastPoint = CGPointMake(cell.myLabel.frame.origin.x + (int)sz.width % (int)linesSz.width,linesSz.height - sz.height);
}
NSLog(@"====%f",lastPoint.x);
[cell.myButton setTitle:@"查看" forState:UIControlStateNormal];
[cell.myButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
if ([cell.myButton.titleLabel.text isEqualToString:@"查看"]) {
cell.myButton.frame = CGRectMake(lastPoint.x+5, labelSize.height-19, 30, 20);
}
[cell.timeButton setTitle:@"今天" forState:UIControlStateNormal];
cell.timeButton.frame = CGRectMake(260, labelSize.height+8, 50, 20);
return cell;
}
类似QQ浏览器滑动
public class test extends Activity implements OnClickListener, SensorEventListener { private SensorManager mSensorManager; @Override protected void onCreate(Bundle savedInstanceState) { //.............. } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } private float[] temp; @Override public void onSensorChanged(SensorEvent event) { if(PublicUtil.isWallPaperLocked(this))return; //屏幕如果锁定 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { if (temp!=null&&isShake(event.values)) { if (!isShaking) { isShaking = true; Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub getData(); } }; timer.schedule(task, 1000);//延迟一秒执行 } } temp = event.values.clone();//克隆传感器里的values的值 } } boolean isShake(float[] values){ boolean flag = false; for (int i = 0; i < values.length; i++) { if(temp[i]-values[i]>15){ //如果摇动的力度大于15 就执行摇一摇 System.out.println("temp="+temp[i]+"value="+values[i]); flag = true; break; } } return flag; } @Override protected void onResume() { super.onResume(); StatService.onResume(this); // 加速度传感器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), // 还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等, // 根据不同应用,需要的反应速率不同,具体根据实际情况设定 SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); StatService.onPause(this); mSensorManager.unregisterListener(this); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); // this.unRegisterLocationListener(); mSensorManager.unregisterListener(this); }
从这个示例,我们可以基本总结出使用Android传感器的步骤,注册一个SensorEventListener,然后在onSensorChanged方法中处理传感器的数据。
这里需要注意的一点是,我把注册Sensor这个过程放在了onResume()方法中,而取消注册放在了onPause()方法中,这主要是因为,传感器的使用太耗电了,而且一定需要手动取消注册,否则即使Activity不可见,传感器依然在工作中,几个小时内就会耗光电量。
更多传感器的知识参考
http://aliusa.iteye.com/blog/1064560