我有UITableView,我正在从服务器填充whit数据。我实现了某种分页,所以我去服务器并获取7个项目,然后当我向下滚动时,我再获取7个,依此类推。这很好用,但我想在最后一个单元格上添加UIActivityIndicatorView,以便用户看到并非所有数据都已下载。我的问题是UIActivityIndicatorView没有旋转。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.needPagin)
return allData.count+1;
return allData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UIActivityIndicatorView * indicator = nil;
NSString *CellIdentifier = @"ResultCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
.....
if (allData.count> indexPath.row) {
[indicator stopAnimating];
indicator.hidden = YES;
}
else{
indicator.hidden = NO;
[indicator startAnimating];
}
return cell;
}
UIActivityIndicatorView显示正常,但没有旋转。 任何人都可以帮我解决这个问题吗?
编辑:
完整代码是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setBackgroundView:nil];
tableView.backgroundColor = [UIColor clearColor];
tableView.separatorColor = [Color separatorColor];
UITableViewCell *cell;
UILabel * titleLable = nil;
UILabel * addressLable = nil;
UILabel * emailLable = nil;
UILabel * phoneLable = nil;
UIImageView * icon = nil;
UIImageView * star = nil;
UIImageView * icon1 = nil;
UIImageView * icon2 = nil;
UIActivityIndicatorView * indicator = nil;
NSString *CellIdentifier = @"ResultCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ResultCell" owner:self options:nil];
cell = self.resultCell;
self.resultCell = nil;
if (!star)
star = (UIImageView*)[cell viewWithTag:5];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addToFavorites:)];
[star addGestureRecognizer:tap];
star.userInteractionEnabled = YES;
titleLable = (UILabel*)[cell viewWithTag:1];
addressLable = (UILabel*)[cell viewWithTag:2];
emailLable = (UILabel*)[cell viewWithTag:3];
phoneLable = (UILabel*)[cell viewWithTag:6];
icon = (UIImageView*)[cell viewWithTag:4];
star = (UIImageView*)[cell viewWithTag:5];
indicator = (UIActivityIndicatorView*)[cell viewWithTag:9];
[titleLable setFont:[UIFont fontWithName:[Fonts mainFontName] size:13]];
[titleLable setTextColor:[Color cellButtonColor]];
[addressLable setFont:[UIFont fontWithName:[Fonts mainFontName] size:12]];
[addressLable setTextColor:[Color cellButtonColor]];
[emailLable setFont:[UIFont fontWithName:[Fonts mainFontName] size:10]];
[emailLable setTextColor:[Color secondButtonColor]];
[emailLable setAdjustsFontSizeToFitWidth:YES];
[phoneLable setFont:[UIFont fontWithName:[Fonts mainFontName] size:10]];
[phoneLable setTextColor:[Color secondButtonColor]];
[phoneLable setAdjustsFontSizeToFitWidth:YES];
[indicator setColor:[Color mainButtonColor]];
}
if (!titleLable)
titleLable = (UILabel*)[cell viewWithTag:1];
if (!addressLable)
addressLable = (UILabel*)[cell viewWithTag:2];
if (!emailLable)
emailLable = (UILabel*)[cell viewWithTag:3];
if (!icon)
icon = (UIImageView*)[cell viewWithTag:4];
if (!star)
star = (UIImageView*)[cell viewWithTag:5];
if (!phoneLable)
phoneLable = (UILabel*)[cell viewWithTag:6];
if (!icon1)
icon1 = (UIImageView*)[cell viewWithTag:7];
if (!icon2)
icon2 = (UIImageView*)[cell viewWithTag:8];
if (!indicator)
indicator = (UIActivityIndicatorView*)[cell viewWithTag:9];
int sportID = -1;
FacilitiesObject * fac;
OrganisationObject * org;
RutesObject *rut;
EventsObject * eve;
id obj_;
if (allData.count> indexPath.row) {
titleLable.hidden = NO;
addressLable.hidden = NO;
emailLable.hidden = NO;
icon.hidden = NO;
star.hidden = NO;
phoneLable.hidden = NO;
icon1.hidden = NO;
icon2.hidden = NO;
[indicator stopAnimating];
indicator.hidden = YES;
switch (self.state) {
case _sportObject_:
fac = [allData objectAtIndex:indexPath.row];
[titleLable setText:fac.name];
[addressLable setText:fac.address];
[emailLable setText:fac.email];
[phoneLable setText:fac.tel];
if (self.filter.sport) {
sportID = self.filter.sport.picture;
}
else if (fac.sports.count>0) {
sportID = [[fac.sports objectAtIndex:0]intValue];
}
if (fac.favourites)
[star setImage:[UIImage imageNamed:@"favourite_on"]];
else
[star setImage:[UIImage imageNamed:@"favourite_off"]];
obj_ = fac;
break;
case _sportOrg_:
org= [allData objectAtIndex:indexPath.row];
[titleLable setText:org.name];
[addressLable setText:org.address];
[emailLable setText:org.email];
[phoneLable setText:org.tel];
if (self.filter.sport) {
sportID = self.filter.sport.picture;
}
else if (org.sports.count>0) {
sportID = [[org.sports objectAtIndex:0]intValue];
}
if (org.favourites)
[star setImage:[UIImage imageNamed:@"favourite_on"]];
else
[star setImage:[UIImage imageNamed:@"favourite_off"]];
obj_ = org;
break;
case _gpsRoots_:
rut = [allData objectAtIndex:indexPath.row];
[titleLable setText:rut.name];
[addressLable setText:[NSString stringWithFormat:@"Dolžina : %i", rut.langth]];
[emailLable setText:[NSString stringWithFormat:@"%i", rut.altitude]];
[phoneLable setText:[NSString stringWithFormat:@"%i", rut.duration]];
if (self.filter.sport) {
sportID = self.filter.sport.picture;
}
else if (rut.sports.count>0) {
sportID = [[rut.sports objectAtIndex:0]intValue];
}
if (rut.favourites)
[star setImage:[UIImage imageNamed:@"favourite_on"]];
else
[star setImage:[UIImage imageNamed:@"favourite_off"]];
break;
case _eventEvent_:
eve = [allData objectAtIndex:indexPath.row];
[titleLable setText:eve.name];
[addressLable setText:eve.address];
[emailLable setText:eve.email];
[phoneLable setText:eve.tel];
if (self.filter.sport) {
sportID = self.filter.sport.picture;
}
else if (eve.sports.count>0) {
sportID = [[eve.sports objectAtIndex:0]intValue];
}
if (eve.favourites)
[star setImage:[UIImage imageNamed:@"favourite_on"]];
else
[star setImage:[UIImage imageNamed:@"favourite_off"]];
obj_ = eve;
break;
case _exercice_:
eve = [allData objectAtIndex:indexPath.row];
[titleLable setText:eve.name];
[addressLable setText:eve.address];
[emailLable setText:eve.email];
[phoneLable setText:eve.tel];
if (self.filter.sport) {
sportID = self.filter.sport.picture;
}
else if (eve.sports.count>0) {
sportID = [[eve.sports objectAtIndex:0]intValue];
}
if (eve.favourites)
[star setImage:[UIImage imageNamed:@"favourite_on"]];
else
[star setImage:[UIImage imageNamed:@"favourite_off"]];
obj_ = eve;
break;
case _competition_:
eve = [allData objectAtIndex:indexPath.row];
[titleLable setText:eve.name];
[addressLable setText:eve.address];
[emailLable setText:eve.email];
[phoneLable setText:eve.tel];
if (self.filter.sport) {
sportID = self.filter.sport.picture;
}
else if (eve.sports.count>0) {
sportID = [[eve.sports objectAtIndex:0]intValue];
}
if (eve.favourites)
[star setImage:[UIImage imageNamed:@"favourite_on"]];
else
[star setImage:[UIImage imageNamed:@"favourite_off"]];
obj_ = eve;
break;
default:
break;
}
[icon setImage:[UIImage imageNamed:[SportsObject sportImageName:sportID]]];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
else{
titleLable.hidden = YES;
addressLable.hidden = YES;
emailLable.hidden = YES;
icon.hidden = YES;
star.hidden = YES;
phoneLable.hidden = YES;
icon1.hidden = YES;
icon2.hidden = YES;
indicator.hidden = NO;
[indicator startAnimating];
}
//
// load next page
if(indexPath.row == allData.count-4 && self.needPagin)
[self nextPage];
return cell;
}
所有UI都是在XIB中制作的。
答案 0 :(得分:0)
你正在制作一些零的动画。您必须实例化微调器,然后将其添加为单元视图的子视图。
UIActivityIndicator *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell.view addSubview:indicator];