简体   繁体   English

如何在UITableView上添加UIActivityIndi​​cator

[英]how i can add UIActivityIndicator on UITableView

i want to attech UIActivityIndicator with UITableView when then first time table load on iphone activity must start blinking and when the table compleletely load in iphone when i click on load more cell then also i want to show activity blink for that moment when more rows loading kindly tell me how can i do that this is the code if you complete this code by your self then thanks in advance 我想使用UITableView使用atTable UIActivityIndicator时,第一次在iPhone活动上加载表必须开始闪烁,并且当我单击加载更多单元格时该表完全加载到iPhone中时,我还想在更多行加载时显示活动在那一刻闪烁告诉我我该怎么办?如果您自己完成此代码,那么请先感谢

#import "RootViewController.h"
#import "Tweet.h"


@implementation RootViewController

@synthesize customImage,pagecontrol,cell;//indexPath;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad 
{
    pageSize = 2;

       // totalpages =(NSInteger) (xmlParser.tweets)/5 ;
    xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://api.twitter.com/1/statuses/user_timeline/KentFranks.xml"];


    [super viewDidLoad];

    self.title = @"Tweets";

}





#pragma mark -
#pragma mark Table view data source


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



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if([xmlParser.tweets count]>pageSize)
    {
        return pageSize+1;
    }
    return  xmlParser.tweets.count;
    }



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    }


    //UIImage    *twitterLogo = [[UIImage imageNamed:@"twitter-logo.png"]autorelease];

    NSString *imagepath = [[NSBundle mainBundle] pathForResource:@"twitter-logo" ofType:@"png"];

    UIImage *image =[[ UIImage alloc] initWithContentsOfFile:imagepath];
    cell.imageView.image = image;



    cell.detailTextLabel.text= @"Add Subtitle here";

    Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
    if(indexPath.row<pageSize)

    {
        cell.textLabel.text = currentTweet.content;
    }

    else
    {


cell.textLabel.text = @"Load more ";
}


        NSLog(@"cell: %@",cell);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;


}

-(IBAction)loadmorebutton:(id)sender;
{

    NSIndexPath *currentPath = [self.tableView indexPathForSelectedRow];
    NSIndexPath *nextPath = [NSIndexPath indexPathForRow:currentPath.row+1 inSection:currentPath.section];
    [self.tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath{


    if (indexPath.row==pageSize) 
    {
        pageSize=pageSize+2;
        [tableView reloadData];

    }

}

/*
 -(IBAction)pagecontrol:(UIPageControl *)pageControl
 {
    [self.tableView reloadData];
}
 -(IBAction)Pageindexchanges:(id)sender
{
    NSLog(@"clicked page index: %i ",pagecontrol.currentPage);

}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

#pragma mark -
#pragma mark Table view delegate

/*- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

}
*/

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning 
{

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload 
{

}


- (void)dealloc 
{
    [xmlParser release];
    [super dealloc];
}


@end

Try this code may be helped you... 尝试此代码可能会帮助您...

Add the spinner directly to the cell view felt a little hacky, so I used a 1x1 transparent png as the image view and resized it to be whatever my spinner size is: 将微调器直接添加到单元格视图时感觉有些棘手,所以我使用了1x1透明png作为图像视图,并将其调整为我的微调器大小:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"NoReuse"] autorelease];
cell.textLabel.text = @"Loading...";

UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] 
     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];

// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:@"spacer"];

UIGraphicsBeginImageContext(spinner.frame.size);

[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];

return cell;

That gives a cell that looks like this: 这样得到的单元格如下所示:

加载单元

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM