我正在我的应用程序中制作一个收件箱模块,第一次加载时有20条消息来自服务器。
我希望在20条消息之后,将在UItableview单元格中创建一个名为“加载更多消息”的标签。
在单击该标签后,我想再次拨打服务器。
我试过下面的代码,但是它不起作用。预先感谢:(
下面是我的cellForRowAtIndexPath和numberOfRowsInSection的示例代码
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [inboxmessagesarray count]+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = @"cell";
__block tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}if(indexPath.row == [self tableView:self.activitiesTableView_ numberOfRowsInSection:indexPath.section] - 1){
cell.textLabel.text=@"Load More Record";// here i am making a label but it is not showing at the end of tableview cell
}
else{
__block NSString *row = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
cell.titlename.font=[UIFont fontWithName:@"SegoeUI" size:15];
cell.tolbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
cell.fromlbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
cell.datelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
cell.timelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
if([[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
{
cell.titlename.font=[UIFont fontWithName:@"SegoeUI" size:15];
cell.tolbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
cell.fromlbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
cell.datelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
cell.timelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
cell.contentView.backgroundColor=[UIColor lightGrayColor];
}
else
{
cell.titlename.font=[UIFont fontWithName:@"SegoeUI" size:15];
cell.tolbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
cell.fromlbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
cell.datelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
cell.timelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
cell.contentView.backgroundColor=[UIColor clearColor];
}
cell.titlename.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerTitle"];
//toCity
cell.tolbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"toCity"];
cell.fromlbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"fromCity"];
if(![[imagesDictionary allKeys] containsObject:row]) //if image not found download and add it to dictionary
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSString *img=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerPhoto"];// here i am getting image path
NSURL *url = [NSURL URLWithString:img];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^{ //in main thread update the image
[imagesDictionary setObject:image forKey:row];
cell.profileimage.image = image;
cell.textLabel.text = @""; //add this update will reflect the changes
NSLog(@"loading and adding to dictionary");
});
});
}
else
{
cell.profileimage.image = [imagesDictionary objectForKey:row];
NSLog(@"retriving from dictionary");
}
cell.datelbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageDate"];
cell.timelbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageTime"];
}
return cell;
}
最佳答案
您可以在多种类型中使用它在这里我添加3种类型,自定义您自己
选择1号
假设您从服务器获取所有数据(例如100),存储在某个NSMutableArray
中,则概念是声明一个全局int
int loadMoreItems;
在你的
viewWillAppear
中 -(void)viewWillAppear:(BOOL)animated
{
loadMoreItems=21; // initially set the count is more than 20
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([yourTotalArrayName count]<=loadMoreItems) {
return [yourTotalArrayName count];
}
else {
return loadMoreItems;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
// add your data
if (indexPath.row+1<loadMoreItems)
{
cell.lblLoadMoreItems.hidden=YES;
cell.itemsdetails.text = [NSString stringWithFormat:@"%@",[yourTotalArrayName objectAtIndex:indexPath.row]] ;
}
else
{
cell.itemsdetails.hidden=YES;
cell.lblLoadMoreItems.text = @"Load more results...!";
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row+1<loadMoreItems) {
// here do your stuff here
}
else {
loadMoreItems=loadMoreItems+20;
[self.yourtableview reloadData];
}
选择2号
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Classic start method
// here add your data loaded
if (indexPath.row == [self.yourarrayName count] - 1)
{
cell.lblLoadMoreItems.text = @"Load more results...!";
[self launchReload];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [self.yourarrayName count] - 1)
{
[self launchReload];
}
}
-(void)launchRelaod
{
// call the webservice
[self.yourtableview reloadData];
}
选择否3
use 2 prototype cell in `UItableView`
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [yourarrayname count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *postCellId = @"postCell";
static NSString *moreCellId = @"moreCell";
UITableViewCell *cell = nil;
if ([indexPath row] == [yourarrayname count]) {
cell = [tableView dequeueReusableCellWithIdentifier:moreCellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moreCellId];
}
cell.textLabel.text = @"Load more items...";
cell.textLabel.textColor = [UIColor blueColor];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:postCellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId];
}
// show your Data
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] == [yourarrayname count]) {
// call the web service and reload Data of UItableview
}
else
{
//normall selcftion procedure
}
}
关于ios - 在uitableviewcell中创建UIlabel的操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27596953/