ios - 在 uitableviewcell 中创建 UIlabel 的 Action
<p><p>我正在我的应用程序中制作一个收件箱模块。第一次加载时,有 20 条消息来自服务器。
我希望在 20 条消息之后,将在 UItableview 单元格中创建一个名为“加载更多消息”的标签。
单击该标签后,我想再次调用服务器。
我曾尝试以下代码,但它不工作。提前谢谢:(
<img src="/image/EdS1e.png" alt="It is showing like this i want to show it after recprds has been laoded"/>
下面是我的 cellForRowAtIndexPath 和 numberOfRowsInSection 的示例代码</p>
<pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return +1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = @"cell";
__blocktablecellTableViewCell *cell= ;
if(cell==nil)
{
cell = [ initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}if(indexPath.row == - 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 = ;
cell.titlename.font=;
cell.tolbl.font=;
cell.fromlbl.font=;
cell.datelbl.font=;
cell.timelbl.font=;
if([[objectForKey:@"messageRead"] intValue]==0)
{
cell.titlename.font=;
cell.tolbl.font=;
cell.fromlbl.font=;
cell.datelbl.font=;
cell.timelbl.font=;
cell.contentView.backgroundColor=;
}
else
{
cell.titlename.font=;
cell.tolbl.font=;
cell.fromlbl.font=;
cell.datelbl.font=;
cell.timelbl.font=;
cell.contentView.backgroundColor=;
}
cell.titlename.text=[objectForKey:@"offerTitle"];
//toCity
cell.tolbl.text=[objectForKey:@"toCity"];
cell.fromlbl.text=[objectForKey:@"fromCity"];
if(![ 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=[objectForKey:@"offerPhoto"];// here i am getting image path
NSURL *url = ;
NSData * imageData = ;
UIImage *image = ;
dispatch_sync(dispatch_get_main_queue(), ^{ //in main thread update the image
;
cell.profileimage.image = image;
cell.textLabel.text = @""; //add this update will reflect the changes
NSLog(@"loading and adding to dictionary");
});
});
}
else
{
cell.profileimage.image = ;
NSLog(@"retriving from dictionary");
}
cell.datelbl.text=[objectForKey:@"messageDate"];
cell.timelbl.text=[objectForKey:@"messageTime"];
}
return cell;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>由于您希望将一行添加到显示 <code>Load More Records</code> 的 tableview 中,您可以通过添加页脚 View 或每个最后一行,通过在方法 <code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section</code> 并在 <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 中返回此行的特定单元格:(NSIndexPath *)indexPath</code> </p>
<p>例如在表格 View 中添加一个自定义单元格并添加一个标签并将其文本设置为 <code>Load more record</code> 并将其重用标识符设置为 <code>LabelCell</code></p>
<pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.inboxmessagesarray.count + 1 ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
//;
if(indexPath.row == )
{
NSLog(@"load more records");
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
__blocktablecellTableViewCell *cell= ;
if(cell == nil)
{
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
}
//in the last row
if(indexPath.row == self.inboxmessagesarray.count)
{
cell = ;
if(cell == nil)
{
cell = [initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LabelCell"];
}
if( > 0)
cell.hidden = NO;
else
cell.hidden = YES;
return cell;
}
//..rest of the code
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 uitableviewcell 中创建 UIlabel 的 Action ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27596953/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27596953/
</a>
</p>
页:
[1]