小编P.J*_*iya的帖子

iPhone SDK:如何将NSArray的内容复制到NSMutable数组?

我需要将NSArray的内容复制到NSMutable数组.换句话说,我想将arrayCountryChoices复制到arraySearchResults.有任何想法吗????

//main data
NSArray *arrayCountryChoices;

//search results buffer 
NSMutableArray *arraySearchResults;


//create data 
arrayCountryChoices = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];   

//copy the original array to searchable array ->> THIS  IS NOT WORKING AS EXPECTED
arraySearchResults = [[NSMutableArray alloc] arrayWithArray:arrayCountryChoices];
Run Code Online (Sandbox Code Playgroud)

提前致谢.

iphone

8
推荐指数
1
解决办法
1万
查看次数

更改UIButton背景颜色

我正在以编程方式创建按钮.我想改变按钮背景颜色,同时再次触摸内部,它必须在抬起我的手指后恢复其通常的颜色.....

nine = [UIButton buttonWithType:UIButtonTypeCustom];
[nine setFrame:CGRectMake(15, 105, 65, 40)];
[nine setTitle:@"9" forState:UIControlStateNormal];
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[nine setBackgroundColor:[UIColor cyanColor]];
[nine addTarget:self action:@selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:nine];
Run Code Online (Sandbox Code Playgroud)

//改变背景颜色

-(void)changeButtonBackGroundColor:(id) sender
{
    [nine setBackgroundColor:[UIColor redColor]];
}
Run Code Online (Sandbox Code Playgroud)

这里创建了changeBackgroundColor方法来改变该按钮的颜色.它改变了颜色.

objective-c

7
推荐指数
1
解决办法
1万
查看次数

使用IBAction按钮缩放MapView

我有一个问题.我的当前位置以地图视图显示并居中,但地图区域不会放大.我尝试通过从didUpdateToLocation方法中取出span和region来获取Rob的建议,但我必须没有正确实现它.我不认为它在viewDidLoad中识别我对setRegion的调用,并且我的按钮无法被识别.请检查下面的代码,并指出错误.我的目标是使用IBAction按钮放大和缩小我的位置.

.H

- (IBAction)zoomIn:(id)sender;

- (IBAction)zoomOut:(id)sender;
Run Code Online (Sandbox Code Playgroud)

.m在viewDidLoad中

double miles = 0.5;

MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/69.0;

MKCoordinateRegion region;
region.span = span;

[self.mapView setRegion:region animated:YES];

[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

_mapView.mapType = MKMapTypeSatellite;
Run Code Online (Sandbox Code Playgroud)

.m在我的didUpdateToLocation方法中.

[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
Run Code Online (Sandbox Code Playgroud)

.放大:

- (IBAction)zoomIn:(id)sender 
{
    MKCoordinateSpan span;
    span.latitudeDelta = _mapView.region.span.latitudeDelta * 2;
    span.longitudeDelta = _mapView.region.span.latitudeDelta * 2;
    MKCoordinateRegion region;
    region.span = span;
    region.center = _mapView.region.center;

    [self.mapView setRegion:region animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

.缩小 :

- (IBAction)zoomOut:(id)sender
{
     MKCoordinateSpan span;
     span.latitudeDelta = _mapView.region.span.latitudeDelta / 2;
     span.longitudeDelta = _mapView.region.span.latitudeDelta …
Run Code Online (Sandbox Code Playgroud)

zoom mkmapview ibaction ios ios6

7
推荐指数
1
解决办法
4719
查看次数

如何动态设置UITableView的高度?

我有一个使用该heightForRowAtIndexPath:方法的不同单元格高度的tableview .

我想动态设置一个高度UITableView.目前我正在viewDidLoad使用以下方法设置tableView的尺寸:

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 770, 310, 400)];
self.tableView.dataSource = self;
self.tableView.delegate = self;
Run Code Online (Sandbox Code Playgroud)

我想也许我可以添加这个:self.tableView.contentSize.height;但问题当然是内容大小只在表加载后计算,所以如果我把它放入

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 770, 310, self.tableView.contentSize.height)];
Run Code Online (Sandbox Code Playgroud)

它不起作用.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *description = [photosFromCommentsArray objectAtIndex:indexPath.row];
    //   NSLog(@"description is %@",description);

    if(description == (id)[NSNull null])
    {
        return 70; 
    }
    else
    {
        return 200;
    }
}
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview ios

7
推荐指数
1
解决办法
9324
查看次数

自定义UITableViewCell具有自动布局和附件视图

我有一个自定义表格视图单元格,它使用自动布局,并有一个披露指标作为附件视图.首次显示时,屏幕上单元格的单元格大小完全错误:

正如您所看到的那样,该单元占用了大约1.5个屏幕的空间:

在此输入图像描述

但是,如果我旋转设备并向后旋转它看起来很好:

在此输入图像描述

正如你在这里看到的,我没有做过任何复杂的事情:

在此输入图像描述

我有一个非常非理想的解决方法:

-(void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

但是当你第一次看到屏幕时,这显然会导致"闪光".在更复杂的情况下,闪光灯更加明显.

我有另一个解决方法,但这会导致自动布局异常:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
    cell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines";
    [cell.basicLabel layoutIfNeeded];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

例外:

在此输入图像描述

至少这种方法不会给我UI闪烁.

如果我删除附件视图,它实际上完全正常.

更新:我已经向github添加了一个示例项目:https: //github.com/fwaddle/TableCellAccessoryTest

更新#2:关于这个错误的另一项工作是在代码中布局单元格.我只是尝试在代码中做同样的事情,它没有抛出警告并且工作正常.看起来像一个IB bug.

有任何想法如何解决这个问题?谢谢.

objective-c uitableview ios ios-autolayout uitableviewautomaticdimension

6
推荐指数
1
解决办法
3606
查看次数

基于内容ios的动态单元高度

我正在开发一个在facebook上加载用户帖子的应用程序.所有这些信息都放在了tableview中.点击它时,细胞是可消耗的.目前只有文字.我计算文本的高度,所以我知道细胞扩展时需要多高.

我遇到的问题是需要的不仅仅是文本.如果用户在Facebook上发布视频,则视频也将嵌入到单元格中.但由于高度是根据文本计算的,因此不会为嵌入视频留下空间.我可以在单元格中添加边距,但此边距将应用于单元格.

这是我现在使用的代码:

//Function executes everything within when a certain row in tapped/selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    [self.tableView reloadData];
    [self stopLoading];
    if (selectedIndexPath == indexPath) 
    {         
        selectedIndexPath = nil;
    }
    else 
    {        
        selectedIndexPath = indexPath;
    }          
    [self.tableView deselectRowAtIndexPath : indexPath animated : YES];
    [tableView beginUpdates];
    [tableView endUpdates];
}

//Change heigth of the selected row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row)) 
    {
        labelSize = [[result2 objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13.0] 
                                                                  constrainedToSize:CGSizeMake(220.0f, MAXFLOAT) …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview ios

5
推荐指数
1
解决办法
7367
查看次数

AVAudioPlayer Help:同时播放多个声音,一次停止所有声音,并解决自动参考计数问题

我想创建一个按钮,play一个声音文件,一个buttonstops所有的声音当前正在播放.如果用户button在短时间内单击多个按钮或相同按钮,应用程序应同时播放所有声音.我使用iOS的System Sound Services毫不费力地完成了这项工作.但是,系统声音服务,通过播放声音volumeiPhone's铃声设置为.我现在正在尝试使用,AVAudioPlayer以便用户可以play通过媒体卷发出声音.这是我目前(但未成功)使用播放声音的代码:

-(IBAction)playSound:(id)sender
{
   AVAudioPlayer *audioPlayer;
   NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Hello" ofType:@"wav"];
   audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
   [audioPlayer prepareToPlay];
   [audioPlayer play];
}
Run Code Online (Sandbox Code Playgroud)

每当我在iPhone模拟器中运行此代码时,它都不会播放声音,但会显示大量输出.当我在iPhone上运行时,声音根本无法播放.在做了一些研究和测试之后,我发现audioPlayer变量是由自动参考计数发布的.此外,当audioPlayer变量被定义为我的接口文件中的实例变量和属性时,此代码可以工作,但它不允许我一次播放多个声音.

首先是第一件事:如何使用AVAudioPlayer和坚持使用自动参考计数一次播放无限数量的声音?另外:当这些声音播放时,我如何实现第二种IBAction方法来停止播放所有这些声音?

iphone audio objective-c avaudioplayer ios

4
推荐指数
1
解决办法
1万
查看次数

iOS 7自定义单元格未在表格视图中显示

请耐心等待,我刚刚开始iOS开发.我试图tableViewCell在故事板中显示自定义tableView.我做了以下事情.

.xibtableViewCell它创建了一个新的

在此输入图像描述

然后我为此创建了一个自定义类..h文件看起来像这样

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *thumbnailImageView;
@property (weak, nonatomic) IBOutlet UILabel› *titleLabel;

@end
Run Code Online (Sandbox Code Playgroud)

然后在我的TableViewController.m我导入了CustomTableCell.h,我正在做10行的跟随

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomTableCell";
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
        cell …
Run Code Online (Sandbox Code Playgroud)

objective-c storyboard ios

4
推荐指数
4
解决办法
2万
查看次数

将参数传递给选择器

我在自定义单元格中有一个UIButton,我想在用户按下该按钮时触发一个动作但是我需要知道按下UIButton的哪一行.

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;

    ...

    [button addTarget:self action:@selector(buttonPressedAction:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonPressedAction:(UIButton *)button
{
    //int row = indexPath.row;
    //NSLog(@"ROW: %i",row);
}
Run Code Online (Sandbox Code Playgroud)

如何将indexPath作为参数传递给我的选择器?

iphone cocoa-touch objective-c ipad ios

3
推荐指数
1
解决办法
1185
查看次数

cocos2d使用CCSpeed动态更改动画速度

在简单的游戏中,角色具有大约15帧的步行动画.基于加速度计的速度在任一方向上都会发生变化.我希望动画相应地加速或减速.我意识到,一旦动画初始化,你就无法改变延迟速度,而是需要创建一个新的动画.但由于速度几乎不断变化,如果我继续重新设置动画,它将始终停留在同一帧上.

有没有办法找到动画当前所在的帧,停止它,然后创建一个具有相同帧的新动画,但从最后一个动画结束开始?

否则,是否有人知道如何实现动态变化的动画延迟时间?

编辑:

我尝试使用CCSpeed从Learn Cocos2d 2中的示例执行此操作.以下是代码:

CCRepeatForever *walkLeftRepeat = [CCRepeatForever actionWithAction:[CCAnimation animationWithSpriteFrames:walkLeftFrames]];

self.walkLeft = [CCSpeed actionWithAction:walkLeftRepeat speed:animationWalkingSpeed];
Run Code Online (Sandbox Code Playgroud)

我收到以下警告:不兼容的指针类型将'CCRepeatForever*'发送到'CCActionInterval*'类型的参数

以下是我关注的示例:

CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy];
CCSpeed* speedAction = [CCSpeed actionWithAction:repeat speed:0.5f];
Run Code Online (Sandbox Code Playgroud)

第二次编辑:

然后我尝试了这个:

CCAnimation *walkLeftRepeat = [CCAnimation animationWithSpriteFrames:walkLeftFrames];
CCAnimate *temp = [CCAnimate actionWithAnimation:walkLeftRepeat];
CCAnimate*temp2 = [CCRepeatForever actionWithAction:temp];
self.walkLeft = [CCSpeed actionWithAction:temp2 speed:animationWalkingSpeed];
[_guy runAction:_walkLeft];
Run Code Online (Sandbox Code Playgroud)

最后编辑.通过在ccanimation中添加延迟来解决这个没有出现的问题.

这不会给出任何错误,但没有任何反应.谢谢!

iphone animation cocos2d-iphone

3
推荐指数
1
解决办法
3235
查看次数