我正在我的应用程序中制作一个收件箱模块。第一次加载时,有 20 条消息来自服务器。
我希望在 20 条消息之后,将在 UItableview 单元格中创建一个名为“加载更多消息”的标签。
单击该标签后,我想再次调用服务器。
我曾尝试以下代码,但它不工作。提前谢谢
下面是我的 cellForRowAtIndexPath 和 numberOfRowsInSection 的示例代码
-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
return [inboxmessagesarray count]+1;
}
-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}
Best Answer-推荐答案 strong>
由于您希望将一行添加到显示 Load More Records 的 tableview 中,您可以通过添加页脚 View 或每个最后一行,通过在方法 -(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section 并在 -(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPath 中返回此行的特定单元格NSIndexPath *)indexPath
例如在表格 View 中添加一个自定义单元格并添加一个标签并将其文本设置为 Load more record 并将其重用标识符设置为 LabelCell
-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.inboxmessagesarray.count + 1 ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[self performSegueWithIdentifier:@"segueIdentifier" sender:tableView];
if(indexPath.row == [self.inboxmessagesarray count])
{
NSLog(@"load more records");
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
__block tablecellTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell == nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
}
//in the last row
if(indexPath.row == self.inboxmessagesarray.count)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"LabelCell"];
if(cell == nil)
{
cell = [[tablecellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LabelCell"];
}
if([self.inboxmessagesarray count] > 0)
cell.hidden = NO;
else
cell.hidden = YES;
return cell;
}
//..rest of the code
关于ios - 在 uitableviewcell 中创建 UIlabel 的 Action ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/27596953/
|